diff --git a/src/DataStreams/SQLiteBlockInputStream.cpp b/src/DataStreams/SQLiteBlockInputStream.cpp index 0d63d54ecd6..46748eaab06 100644 --- a/src/DataStreams/SQLiteBlockInputStream.cpp +++ b/src/DataStreams/SQLiteBlockInputStream.cpp @@ -77,8 +77,10 @@ Block SQLiteBlockInputStream::readImpl() case SQLITE_NULL: { const char * data = reinterpret_cast(sqlite3_column_text(compiled_statement.get(), idx)); - if (!data) { - (*columns[idx]).insertFrom(*sample.column, 0); break; + if (!data) + { + (*columns[idx]).insertFrom(*sample.column, 0); + break; } int len = sqlite3_column_bytes(compiled_statement.get(), idx); assert_cast(*columns[idx]).insertData(data, len); diff --git a/src/Databases/SQLite/DatabaseSQLite.cpp b/src/Databases/SQLite/DatabaseSQLite.cpp index e9e37904dbf..df132463c2d 100644 --- a/src/Databases/SQLite/DatabaseSQLite.cpp +++ b/src/Databases/SQLite/DatabaseSQLite.cpp @@ -47,9 +47,9 @@ DatabaseTablesIteratorPtr DatabaseSQLite::getTablesIterator(ContextPtr local_con std::unordered_set DatabaseSQLite::fetchTablesList() const { std::unordered_set tables; - std::string query = "SELECT name FROM sqlite_schema \n" - "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%'\n" - "ORDER BY 1;"; + std::string query = "SELECT name FROM sqlite_master \n" + "WHERE type = 'table' AND \n" + " name NOT LIKE 'sqlite_%'"; auto callback_get_data = [](void * res, int col_num, char ** data_by_col, char ** /* col_names */) -> int { for (int i = 0; i < col_num; ++i) @@ -131,8 +131,7 @@ StoragePtr DatabaseSQLite::fetchTable(const String & table_name, ContextPtr loca table_name, ColumnsDescription{*columns}, ConstraintsDescription{}, - local_context, - ""); + local_context); return storage; } @@ -149,5 +148,4 @@ void DatabaseSQLite::shutdown() { } - } diff --git a/src/Databases/SQLite/fetchSQLiteTableStructure.cpp b/src/Databases/SQLite/fetchSQLiteTableStructure.cpp index 97230935d55..0c4689aae45 100644 --- a/src/Databases/SQLite/fetchSQLiteTableStructure.cpp +++ b/src/Databases/SQLite/fetchSQLiteTableStructure.cpp @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -36,11 +37,13 @@ std::shared_ptr fetchSQLiteTableStructure(sqlite3 * connectio { auto columns = NamesAndTypesList(); - std::string query = fmt::format("pragma table_info({});", sqlite_table_name); + std::string query = fmt::format("pragma table_info({});", quoteString(sqlite_table_name)); auto callback_get_data = [](void * res, int col_num, char ** data_by_col, char ** col_names) -> int { NameAndTypePair name_and_type; + bool is_nullable = false; + for (int i = 0; i < col_num; ++i) { if (strcmp(col_names[i], "name") == 0) @@ -51,8 +54,15 @@ std::shared_ptr fetchSQLiteTableStructure(sqlite3 * connectio { name_and_type.type = convertSQLiteDataType(data_by_col[i]); } + else if (strcmp(col_names[i], "notnull") == 0) + { + is_nullable = (data_by_col[i][0] == '0'); + } } + if (is_nullable) + name_and_type.type = std::make_shared(name_and_type.type); + static_cast(res)->push_back(name_and_type); return 0; diff --git a/src/Storages/StorageSQLite.cpp b/src/Storages/StorageSQLite.cpp index 51588ac6ba3..24adcdab99c 100644 --- a/src/Storages/StorageSQLite.cpp +++ b/src/Storages/StorageSQLite.cpp @@ -26,12 +26,10 @@ StorageSQLite::StorageSQLite( const String & remote_table_name_, const ColumnsDescription & columns_, const ConstraintsDescription & constraints_, - ContextPtr context_, - const String & remote_table_schema_) + ContextPtr context_) : IStorage(table_id_) , WithContext(context_->getGlobalContext()) , remote_table_name(remote_table_name_) - , remote_table_schema(remote_table_schema_) , global_context(context_) , db_ptr(db_ptr_) { @@ -51,13 +49,11 @@ Pipe StorageSQLite::read( { metadata_snapshot->check(column_names, getVirtuals(), getStorageID()); - /// Connection is already made to the needed database, so it should not be present in the query; - /// remote_table_schema is empty if it is not specified, will access only table_name. String query = transformQueryForExternalDatabase( query_info, metadata_snapshot->getColumns().getOrdinary(), IdentifierQuotingStyle::DoubleQuotes, - remote_table_schema, + "", remote_table_name, context_); @@ -79,12 +75,10 @@ public: const StorageSQLite & storage_, const StorageMetadataPtr & metadata_snapshot_, std::shared_ptr connection_, - const std::string & remote_database_name_, const std::string & remote_table_name_) : storage{storage_} , metadata_snapshot(metadata_snapshot_) , connection(connection_) - , remote_database_name{remote_database_name_} , remote_table_name(remote_table_name_) { } @@ -98,16 +92,14 @@ public: { WriteBufferFromOwnString sqlbuf; sqlbuf << "INSERT INTO "; - if (!remote_database_name.empty()) - sqlbuf << "\"" << remote_database_name << "\"."; - sqlbuf << "\"" << remote_table_name << "\""; + sqlbuf << doubleQuoteString(remote_table_name); sqlbuf << " ("; for (auto it = block.begin(); it != block.end(); ++it) { if (it != block.begin()) sqlbuf << ", "; - sqlbuf << "'" << it->name << "'"; + sqlbuf << quoteString(it->name); } sqlbuf << ") VALUES "; @@ -136,13 +128,12 @@ private: const StorageSQLite & storage; StorageMetadataPtr metadata_snapshot; std::shared_ptr connection; - std::string remote_database_name; std::string remote_table_name; }; BlockOutputStreamPtr StorageSQLite::write(const ASTPtr & /* query */, const StorageMetadataPtr & metadata_snapshot, ContextPtr) { - return std::make_shared(*this, metadata_snapshot, db_ptr, "", remote_table_name); + return std::make_shared(*this, metadata_snapshot, db_ptr, remote_table_name); } void registerStorageSQLite(StorageFactory & factory) @@ -175,8 +166,7 @@ void registerStorageSQLite(StorageFactory & factory) table_name, args.columns, args.constraints, - args.getContext(), - ""); + args.getContext()); }, { .source_access_type = AccessType::SQLITE, diff --git a/src/Storages/StorageSQLite.h b/src/Storages/StorageSQLite.h index d7fa5dee316..9d9feb1bd66 100644 --- a/src/Storages/StorageSQLite.h +++ b/src/Storages/StorageSQLite.h @@ -20,8 +20,7 @@ public: const String & remote_table_name_, const ColumnsDescription & columns_, const ConstraintsDescription & constraints_, - ContextPtr context_, - const String & remote_table_schema_); + ContextPtr context_); std::string getName() const override { return "SQLite"; } @@ -38,7 +37,6 @@ public: private: String remote_table_name; - String remote_table_schema; ContextPtr global_context; std::shared_ptr db_ptr; };