| 182 | |
| 183 | template <typename LogElement> |
| 184 | bool prepareDatabaseAndTable( |
| 185 | const ContextPtr & context, |
| 186 | const String & database_name, |
| 187 | const String & table_name, |
| 188 | const Poco::Util::AbstractConfiguration & config, |
| 189 | const String & config_prefix, |
| 190 | Poco::Logger * log) |
| 191 | { |
| 192 | if (!config.has(config_prefix) || |
| 193 | (context->getServerType() != ServerType::cnch_server)) |
| 194 | return true; |
| 195 | |
| 196 | if (!createDatabaseInCatalog(context, database_name, log)) |
| 197 | return false; |
| 198 | |
| 199 | auto create = std::make_shared<ASTCreateQuery>(); |
| 200 | create->database = database_name; |
| 201 | create->table = table_name; |
| 202 | |
| 203 | auto ordinary_columns = LogElement::getNamesAndTypes(); |
| 204 | auto alias_columns = LogElement::getNamesAndAliases(); |
| 205 | auto new_columns_list = std::make_shared<ASTColumns>(); |
| 206 | new_columns_list->set(new_columns_list->columns, InterpreterCreateQuery::formatColumns(ordinary_columns, alias_columns, ParserSettings::CLICKHOUSE)); |
| 207 | create->set(create->columns_list, new_columns_list); |
| 208 | |
| 209 | String engine; |
| 210 | if (config.has(config_prefix + ".engine")) |
| 211 | { |
| 212 | if (config.has(config_prefix + ".partition_by")) |
| 213 | throw Exception("If 'engine' is specified for system table, " |
| 214 | "PARTITION BY parameters should be specified directly inside 'engine' and 'partition_by' setting doesn't make sense", |
| 215 | ErrorCodes::BAD_ARGUMENTS); |
| 216 | if (config.has(config_prefix + ".ttl")) |
| 217 | throw Exception("If 'engine' is specified for system table, " |
| 218 | "TTL parameters should be specified directly inside 'engine' and 'ttl' setting doesn't make sense", |
| 219 | ErrorCodes::BAD_ARGUMENTS); |
| 220 | engine = config.getString(config_prefix + ".engine"); |
| 221 | } |
| 222 | else |
| 223 | { |
| 224 | engine = prepareEngineClause<LogElement>(config, config_prefix); |
| 225 | } |
| 226 | |
| 227 | ASTPtr create_query = constructCreateTableQuery<LogElement>(database_name, table_name, engine); |
| 228 | |
| 229 | if (!prepareCnchTable(context, database_name, table_name, create_query, log)) |
| 230 | return false; |
| 231 | |
| 232 | ColumnsWithTypeAndName log_element_columns; |
| 233 | for (const auto & name_and_type : ordinary_columns) |
| 234 | log_element_columns.emplace_back(name_and_type.type, name_and_type.name); |
| 235 | |
| 236 | Block expected_block(std::move(log_element_columns)); |
| 237 | |
| 238 | if (!syncTableSchema(context, database_name, table_name, expected_block, log)) |
| 239 | return false; |
| 240 | if (!createView(context, database_name, table_name, log)) |
| 241 | return false; |
nothing calls this directly
no test coverage detected