| 175 | } |
| 176 | |
| 177 | ASTPtr DatabaseMySQL::getCreateTableQueryImpl(const String & table_name, ContextPtr local_context, bool throw_on_error) const |
| 178 | { |
| 179 | std::lock_guard lock(mutex); |
| 180 | |
| 181 | try |
| 182 | { |
| 183 | /// This function can throw mysql exception, we don't have enough context to handle it. |
| 184 | /// So we just catch and re-throw as known exception if needed. |
| 185 | fetchTablesIntoLocalCache(local_context); |
| 186 | } |
| 187 | catch (...) |
| 188 | { |
| 189 | if (throw_on_error) |
| 190 | { |
| 191 | throw Exception(ErrorCodes::CANNOT_GET_CREATE_TABLE_QUERY, |
| 192 | "Received error while fetching table structure for table {} from MySQL: {}", |
| 193 | backQuote(table_name), getCurrentExceptionMessage(true)); |
| 194 | } |
| 195 | |
| 196 | tryLogCurrentException(__PRETTY_FUNCTION__); |
| 197 | } |
| 198 | |
| 199 | if (!local_tables_cache.contains(table_name)) |
| 200 | { |
| 201 | if (throw_on_error) |
| 202 | throw Exception(ErrorCodes::UNKNOWN_TABLE, "MySQL table {}.{} doesn't exist.", database_name_in_mysql, table_name); |
| 203 | return nullptr; |
| 204 | } |
| 205 | |
| 206 | auto storage = local_tables_cache[table_name].second; |
| 207 | auto table_storage_define = database_engine_define->clone(); |
| 208 | { |
| 209 | ASTStorage * ast_storage = table_storage_define->as<ASTStorage>(); |
| 210 | ast_storage->engine->setKind(ASTFunction::Kind::TABLE_ENGINE); |
| 211 | auto storage_engine_arguments = ast_storage->engine->arguments; |
| 212 | |
| 213 | /// Add table_name to engine arguments |
| 214 | if (typeid_cast<ASTIdentifier *>(storage_engine_arguments->children[0].get())) |
| 215 | { |
| 216 | storage_engine_arguments->children.push_back( |
| 217 | makeASTOperator("equals", make_intrusive<ASTIdentifier>("table"), make_intrusive<ASTLiteral>(table_name))); |
| 218 | } |
| 219 | else |
| 220 | { |
| 221 | auto mysql_table_name = make_intrusive<ASTLiteral>(table_name); |
| 222 | storage_engine_arguments->children.insert(storage_engine_arguments->children.begin() + 2, mysql_table_name); |
| 223 | } |
| 224 | |
| 225 | /// Unset settings |
| 226 | ast_storage->reset(ast_storage->settings); |
| 227 | } |
| 228 | |
| 229 | const Settings & settings = getContext()->getSettingsRef(); |
| 230 | auto create_table_query = DB::getCreateQueryFromStorage( |
| 231 | storage, |
| 232 | table_storage_define, |
| 233 | true, |
| 234 | static_cast<unsigned>(settings[Setting::max_parser_depth]), |
nothing calls this directly
no test coverage detected