Some review fixes

This commit is contained in:
Arslan G 2021-06-01 23:52:12 +03:00
parent fbc5734302
commit bf4171e0ea
5 changed files with 26 additions and 28 deletions

View File

@ -77,8 +77,10 @@ Block SQLiteBlockInputStream::readImpl()
case SQLITE_NULL: case SQLITE_NULL:
{ {
const char * data = reinterpret_cast<const char *>(sqlite3_column_text(compiled_statement.get(), idx)); const char * data = reinterpret_cast<const char *>(sqlite3_column_text(compiled_statement.get(), idx));
if (!data) { if (!data)
(*columns[idx]).insertFrom(*sample.column, 0); break; {
(*columns[idx]).insertFrom(*sample.column, 0);
break;
} }
int len = sqlite3_column_bytes(compiled_statement.get(), idx); int len = sqlite3_column_bytes(compiled_statement.get(), idx);
assert_cast<ColumnString &>(*columns[idx]).insertData(data, len); assert_cast<ColumnString &>(*columns[idx]).insertData(data, len);

View File

@ -47,9 +47,9 @@ DatabaseTablesIteratorPtr DatabaseSQLite::getTablesIterator(ContextPtr local_con
std::unordered_set<std::string> DatabaseSQLite::fetchTablesList() const std::unordered_set<std::string> DatabaseSQLite::fetchTablesList() const
{ {
std::unordered_set<String> tables; std::unordered_set<String> tables;
std::string query = "SELECT name FROM sqlite_schema \n" std::string query = "SELECT name FROM sqlite_master \n"
"WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%'\n" "WHERE type = 'table' AND \n"
"ORDER BY 1;"; " name NOT LIKE 'sqlite_%'";
auto callback_get_data = [](void * res, int col_num, char ** data_by_col, char ** /* col_names */) -> int { 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) for (int i = 0; i < col_num; ++i)
@ -131,8 +131,7 @@ StoragePtr DatabaseSQLite::fetchTable(const String & table_name, ContextPtr loca
table_name, table_name,
ColumnsDescription{*columns}, ColumnsDescription{*columns},
ConstraintsDescription{}, ConstraintsDescription{},
local_context, local_context);
"");
return storage; return storage;
} }
@ -149,5 +148,4 @@ void DatabaseSQLite::shutdown()
{ {
} }
} }

View File

@ -1,5 +1,6 @@
#include <Databases/SQLite/fetchSQLiteTableStructure.h> #include <Databases/SQLite/fetchSQLiteTableStructure.h>
#include <Common/quoteString.h>
#include <DataTypes/DataTypeArray.h> #include <DataTypes/DataTypeArray.h>
#include <DataTypes/DataTypeDate.h> #include <DataTypes/DataTypeDate.h>
#include <DataTypes/DataTypeDateTime.h> #include <DataTypes/DataTypeDateTime.h>
@ -36,11 +37,13 @@ std::shared_ptr<NamesAndTypesList> fetchSQLiteTableStructure(sqlite3 * connectio
{ {
auto columns = NamesAndTypesList(); 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 { auto callback_get_data = [](void * res, int col_num, char ** data_by_col, char ** col_names) -> int {
NameAndTypePair name_and_type; NameAndTypePair name_and_type;
bool is_nullable = false;
for (int i = 0; i < col_num; ++i) for (int i = 0; i < col_num; ++i)
{ {
if (strcmp(col_names[i], "name") == 0) if (strcmp(col_names[i], "name") == 0)
@ -51,8 +54,15 @@ std::shared_ptr<NamesAndTypesList> fetchSQLiteTableStructure(sqlite3 * connectio
{ {
name_and_type.type = convertSQLiteDataType(data_by_col[i]); 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<DataTypeNullable>(name_and_type.type);
static_cast<NamesAndTypesList *>(res)->push_back(name_and_type); static_cast<NamesAndTypesList *>(res)->push_back(name_and_type);
return 0; return 0;

View File

@ -26,12 +26,10 @@ StorageSQLite::StorageSQLite(
const String & remote_table_name_, const String & remote_table_name_,
const ColumnsDescription & columns_, const ColumnsDescription & columns_,
const ConstraintsDescription & constraints_, const ConstraintsDescription & constraints_,
ContextPtr context_, ContextPtr context_)
const String & remote_table_schema_)
: IStorage(table_id_) : IStorage(table_id_)
, WithContext(context_->getGlobalContext()) , WithContext(context_->getGlobalContext())
, remote_table_name(remote_table_name_) , remote_table_name(remote_table_name_)
, remote_table_schema(remote_table_schema_)
, global_context(context_) , global_context(context_)
, db_ptr(db_ptr_) , db_ptr(db_ptr_)
{ {
@ -51,13 +49,11 @@ Pipe StorageSQLite::read(
{ {
metadata_snapshot->check(column_names, getVirtuals(), getStorageID()); 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( String query = transformQueryForExternalDatabase(
query_info, query_info,
metadata_snapshot->getColumns().getOrdinary(), metadata_snapshot->getColumns().getOrdinary(),
IdentifierQuotingStyle::DoubleQuotes, IdentifierQuotingStyle::DoubleQuotes,
remote_table_schema, "",
remote_table_name, remote_table_name,
context_); context_);
@ -79,12 +75,10 @@ public:
const StorageSQLite & storage_, const StorageSQLite & storage_,
const StorageMetadataPtr & metadata_snapshot_, const StorageMetadataPtr & metadata_snapshot_,
std::shared_ptr<sqlite3> connection_, std::shared_ptr<sqlite3> connection_,
const std::string & remote_database_name_,
const std::string & remote_table_name_) const std::string & remote_table_name_)
: storage{storage_} : storage{storage_}
, metadata_snapshot(metadata_snapshot_) , metadata_snapshot(metadata_snapshot_)
, connection(connection_) , connection(connection_)
, remote_database_name{remote_database_name_}
, remote_table_name(remote_table_name_) , remote_table_name(remote_table_name_)
{ {
} }
@ -98,16 +92,14 @@ public:
{ {
WriteBufferFromOwnString sqlbuf; WriteBufferFromOwnString sqlbuf;
sqlbuf << "INSERT INTO "; sqlbuf << "INSERT INTO ";
if (!remote_database_name.empty()) sqlbuf << doubleQuoteString(remote_table_name);
sqlbuf << "\"" << remote_database_name << "\".";
sqlbuf << "\"" << remote_table_name << "\"";
sqlbuf << " ("; sqlbuf << " (";
for (auto it = block.begin(); it != block.end(); ++it) for (auto it = block.begin(); it != block.end(); ++it)
{ {
if (it != block.begin()) if (it != block.begin())
sqlbuf << ", "; sqlbuf << ", ";
sqlbuf << "'" << it->name << "'"; sqlbuf << quoteString(it->name);
} }
sqlbuf << ") VALUES "; sqlbuf << ") VALUES ";
@ -136,13 +128,12 @@ private:
const StorageSQLite & storage; const StorageSQLite & storage;
StorageMetadataPtr metadata_snapshot; StorageMetadataPtr metadata_snapshot;
std::shared_ptr<sqlite3> connection; std::shared_ptr<sqlite3> connection;
std::string remote_database_name;
std::string remote_table_name; std::string remote_table_name;
}; };
BlockOutputStreamPtr StorageSQLite::write(const ASTPtr & /* query */, const StorageMetadataPtr & metadata_snapshot, ContextPtr) BlockOutputStreamPtr StorageSQLite::write(const ASTPtr & /* query */, const StorageMetadataPtr & metadata_snapshot, ContextPtr)
{ {
return std::make_shared<SQLiteBlockOutputStream>(*this, metadata_snapshot, db_ptr, "", remote_table_name); return std::make_shared<SQLiteBlockOutputStream>(*this, metadata_snapshot, db_ptr, remote_table_name);
} }
void registerStorageSQLite(StorageFactory & factory) void registerStorageSQLite(StorageFactory & factory)
@ -175,8 +166,7 @@ void registerStorageSQLite(StorageFactory & factory)
table_name, table_name,
args.columns, args.columns,
args.constraints, args.constraints,
args.getContext(), args.getContext());
"");
}, },
{ {
.source_access_type = AccessType::SQLITE, .source_access_type = AccessType::SQLITE,

View File

@ -20,8 +20,7 @@ public:
const String & remote_table_name_, const String & remote_table_name_,
const ColumnsDescription & columns_, const ColumnsDescription & columns_,
const ConstraintsDescription & constraints_, const ConstraintsDescription & constraints_,
ContextPtr context_, ContextPtr context_);
const String & remote_table_schema_);
std::string getName() const override { return "SQLite"; } std::string getName() const override { return "SQLite"; }
@ -38,7 +37,6 @@ public:
private: private:
String remote_table_name; String remote_table_name;
String remote_table_schema;
ContextPtr global_context; ContextPtr global_context;
std::shared_ptr<sqlite3> db_ptr; std::shared_ptr<sqlite3> db_ptr;
}; };