diff --git a/src/observer/virtual_table/ob_show_create_database.cpp b/src/observer/virtual_table/ob_show_create_database.cpp index 73863a16a1..d4e26d28a6 100644 --- a/src/observer/virtual_table/ob_show_create_database.cpp +++ b/src/observer/virtual_table/ob_show_create_database.cpp @@ -110,6 +110,8 @@ int ObShowCreateDatabase::fill_row_cells(uint64_t show_database_id, { int ret = OB_SUCCESS; bool strict_mode = false; + bool sql_quote_show_create = true; + bool ansi_quotes = false; if (OB_ISNULL(cur_row_.cells_) || OB_ISNULL(schema_guard_) || OB_ISNULL(allocator_) @@ -125,6 +127,10 @@ int ObShowCreateDatabase::fill_row_cells(uint64_t show_database_id, K(output_column_ids_.count())); } else if (OB_FAIL(session_->get_show_ddl_in_compat_mode(strict_mode))) { SERVER_LOG(WARN, "failed to get _show_ddl_in_compat_mode", K(ret)); + } else if (OB_FAIL(session_->get_sql_quote_show_create(sql_quote_show_create))) { + SERVER_LOG(WARN, "failed to get sql_quote_show_create", K(ret)); + } else if (OB_FALSE_IT(IS_ANSI_QUOTES(session_->get_sql_mode(), ansi_quotes))) { + // do nothing } else { uint64_t cell_idx = 0; char *db_def_buf = NULL; @@ -150,7 +156,7 @@ int ObShowCreateDatabase::fill_row_cells(uint64_t show_database_id, } case OB_APP_MIN_COLUMN_ID + 2: { // create_database - ObSchemaPrinter schema_printer(*schema_guard_, strict_mode); + ObSchemaPrinter schema_printer(*schema_guard_, strict_mode, sql_quote_show_create, ansi_quotes); int64_t pos = 0; if (OB_FAIL(schema_printer.print_database_definiton(effective_tenant_id_, show_database_id, @@ -171,7 +177,7 @@ int ObShowCreateDatabase::fill_row_cells(uint64_t show_database_id, } case OB_APP_MIN_COLUMN_ID + 3: { // create_database_with_if_not_exists - ObSchemaPrinter schema_printer(*schema_guard_, strict_mode); + ObSchemaPrinter schema_printer(*schema_guard_, strict_mode, sql_quote_show_create, ansi_quotes); int64_t pos = 0; if (OB_FAIL(schema_printer.print_database_definiton(effective_tenant_id_, show_database_id, diff --git a/src/observer/virtual_table/ob_show_create_table.cpp b/src/observer/virtual_table/ob_show_create_table.cpp index d26e407598..98772175d9 100644 --- a/src/observer/virtual_table/ob_show_create_table.cpp +++ b/src/observer/virtual_table/ob_show_create_table.cpp @@ -159,6 +159,8 @@ int ObShowCreateTable::fill_row_cells_inner(const uint64_t show_table_id, bool strict_mode = false; bool is_oracle_mode = false; + bool sql_quote_show_create = true; + bool ansi_quotes = false; if (OB_UNLIKELY(NULL == schema_guard_ || NULL == session_ || NULL == allocator_ @@ -185,9 +187,12 @@ int ObShowCreateTable::fill_row_cells_inner(const uint64_t show_table_id, SERVER_LOG(WARN, "failed to check if oracle mode", K(ret)); } else if (OB_FAIL(session_->get_show_ddl_in_compat_mode(strict_mode))) { SERVER_LOG(WARN, "failed to get _show_ddl_in_compat_mode", K(ret)); + } else if (OB_FAIL(session_->get_sql_quote_show_create(sql_quote_show_create))) { + SERVER_LOG(WARN, "failed to get sql quote show create", K(ret)); } else { //_show_ddl_in_compat_mode do not support oracle mode now strict_mode &= !is_oracle_mode; + IS_ANSI_QUOTES(session_->get_sql_mode(), ansi_quotes); for (int64_t i = 0; OB_SUCC(ret) && i < output_column_ids_.count(); ++i) { uint64_t col_id = output_column_ids_.at(i); switch(col_id) { @@ -205,7 +210,7 @@ int ObShowCreateTable::fill_row_cells_inner(const uint64_t show_table_id, } case OB_APP_MIN_COLUMN_ID + 2: { // create_table - ObSchemaPrinter schema_printer(*schema_guard_, strict_mode); + ObSchemaPrinter schema_printer(*schema_guard_, strict_mode, sql_quote_show_create, ansi_quotes); int64_t pos = 0; if (table_schema.is_view_table()) { if (OB_FAIL(schema_printer.print_view_definiton(effective_tenant_id_, diff --git a/src/share/schema/ob_schema_printer.cpp b/src/share/schema/ob_schema_printer.cpp index 2c61dbc4eb..c57261e741 100644 --- a/src/share/schema/ob_schema_printer.cpp +++ b/src/share/schema/ob_schema_printer.cpp @@ -38,6 +38,8 @@ #include "sql/ob_sql_utils.h" #include "share/ob_get_compat_mode.h" + + namespace oceanbase { namespace share @@ -46,11 +48,18 @@ namespace schema { using namespace std; using namespace common; + /*----------------------------------------------------------------------------- * ObSchemaPrinter *-----------------------------------------------------------------------------*/ -ObSchemaPrinter::ObSchemaPrinter(ObSchemaGetterGuard &schema_guard, bool strict_compat) - : schema_guard_(schema_guard), strict_compat_(strict_compat) +ObSchemaPrinter::ObSchemaPrinter(ObSchemaGetterGuard &schema_guard, + bool strict_compat, + bool sql_quote_show_create, + bool ansi_quotes) + : schema_guard_(schema_guard), + strict_compat_(strict_compat), + sql_quote_show_create_(sql_quote_show_create), + ansi_quotes_(ansi_quotes) { } @@ -107,14 +116,18 @@ int ObSchemaPrinter::print_table_definition(const uint64_t tenant_id, allocator, db_schema->get_database_name_str(), new_db_name, is_oracle_mode))) { SHARE_SCHEMA_LOG(WARN, "fail to generate new name with escape character", K(ret), K(db_schema->get_database_name_str())); - } else if (OB_FAIL(!agent_mode ? - databuff_printf(buf, buf_len, pos, - is_oracle_mode ? "CREATE%s TABLE \"%s\" (\n" : "CREATE%s TABLE `%s` (\n", - prefix_arr[prefix_idx], new_table_name.empty() ? "" : new_table_name.ptr()) - : databuff_printf(buf, buf_len, pos, - is_oracle_mode ? "CREATE%s TABLE \"%s\".\"%s\" (\n" : "CREATE%s TABLE `%s`.`%s` (\n", - prefix_arr[prefix_idx], new_db_name.ptr(), new_table_name.empty() ? "" : new_table_name.ptr()))) { - SHARE_SCHEMA_LOG(WARN, "fail to print create table prefix", K(ret), K(table_schema->get_table_name())); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, "CREATE%s TABLE ", prefix_arr[prefix_idx]))) { + SHARE_SCHEMA_LOG(WARN, "fail to print create table prefix", K(ret)); + } else if (agent_mode && + OB_FAIL(print_identifier(buf, buf_len, pos, new_db_name, is_oracle_mode))) { + SHARE_SCHEMA_LOG(WARN, "fail to print create table prefix identifier", K(ret)); + } else if (agent_mode && + OB_FAIL(databuff_printf(buf, buf_len, pos, "."))) { + SHARE_SCHEMA_LOG(WARN, "fail to print create table prefix", K(ret)); + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, new_table_name, is_oracle_mode))) { + SHARE_SCHEMA_LOG(WARN, "fail to print create table prefix identifier", K(ret)); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, " (\n"))) { + SHARE_SCHEMA_LOG(WARN, "fail to print create table prefix", K(ret)); } } @@ -202,9 +215,11 @@ int ObSchemaPrinter::print_table_definition_columns(const ObTableSchema &table_s new_col_name, is_oracle_mode))) { SHARE_SCHEMA_LOG(WARN, "fail to generate new name with escape character", K(ret), K(col->get_column_name_str())); - } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, - is_oracle_mode ? " \"%s\" " : " `%s` ", - new_col_name.ptr()))) { + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, " "))) { + SHARE_SCHEMA_LOG(WARN, "fail to print column", K(ret), K(*col)); + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, new_col_name, is_oracle_mode))) { + SHARE_SCHEMA_LOG(WARN, "fail to print column", K(ret), K(*col)); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, " "))) { SHARE_SCHEMA_LOG(WARN, "fail to print column", K(ret), K(*col)); } @@ -565,10 +580,13 @@ int ObSchemaPrinter::print_single_index_definition(const ObTableSchema *index_sc } if (OB_FAIL(ret)) { //do nothing - } else if (OB_FAIL(is_oracle_mode ? - databuff_printf(buf, buf_len, pos, "(") : - databuff_printf(buf, buf_len, pos, "`%.*s` (", - new_index_name.length(), new_index_name.ptr()))) { + } else if (!is_oracle_mode && + OB_FAIL(print_identifier(buf, buf_len, pos, new_index_name, is_oracle_mode))) { + SHARE_SCHEMA_LOG(WARN, "fail to print index name", K(ret), K(index_name)); + } else if (!is_oracle_mode && + OB_FAIL(databuff_printf(buf, buf_len, pos, " "))) { + SHARE_SCHEMA_LOG(WARN, "fail to print index name", K(ret), K(index_name)); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, "("))) { SHARE_SCHEMA_LOG(WARN, "fail to print index name", K(ret), K(index_name)); } else { // index columns contain rowkeys of base table, but no need to show them. @@ -632,8 +650,10 @@ int ObSchemaPrinter::print_single_index_definition(const ObTableSchema *index_sc first_storing_column = false; } if (OB_SUCC(ret)) { - if (OB_FAIL(databuff_printf(buf, buf_len, pos, - is_oracle_mode ? "\"%s\", " : "`%s`, ", (*row_col)->get_column_name()))) { + if (OB_FAIL(print_identifier(buf, buf_len, pos, + (*row_col)->get_column_name(), is_oracle_mode))) { + SHARE_SCHEMA_LOG(WARN, "fail to print column name", K(ret), K((*row_col)->get_column_name())); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, ", "))) { SHARE_SCHEMA_LOG(WARN, "fail to print column name", K(ret), K((*row_col)->get_column_name())); } } @@ -792,12 +812,13 @@ int ObSchemaPrinter::print_table_definition_constraints(const ObTableSchema &tab is_oracle_mode))) { SHARE_SCHEMA_LOG(WARN, "fail to generate new name with escape character", K(ret), K(cst->get_constraint_name_str())); } else if (lib::is_mysql_mode() && CONSTRAINT_TYPE_NOT_NULL != cst->get_constraint_type()) { - if (OB_FAIL(databuff_printf(buf, buf_len, pos, - ",\n CONSTRAINT `%.*s` CHECK (%.*s)", - new_cst_name.length(), - new_cst_name.ptr(), - cst->get_check_expr_str().length(), - cst->get_check_expr_str().ptr()))) { + if (OB_FAIL(databuff_printf(buf, buf_len, pos, ",\n CONSTRAINT "))) { + SHARE_SCHEMA_LOG(WARN, "fail to print constraint", K(ret)); + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, new_cst_name, false))) { + SHARE_SCHEMA_LOG(WARN, "fail to print constraint", K(ret)); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, " CHECK (%.*s)", + cst->get_check_expr_str().length(), + cst->get_check_expr_str().ptr()))) { SHARE_SCHEMA_LOG(WARN, "fail to print constraint", K(ret)); } if (OB_SUCC(ret) && !cst->get_enable_flag()) { @@ -806,13 +827,14 @@ int ObSchemaPrinter::print_table_definition_constraints(const ObTableSchema &tab } } } else if (is_oracle_mode && CONSTRAINT_TYPE_CHECK == cst->get_constraint_type()) { - if (OB_FAIL(databuff_printf(buf, buf_len, pos, - ",\n CONSTRAINT \"%.*s\" CHECK (%.*s)", - new_cst_name.length(), - new_cst_name.ptr(), - cst->get_check_expr_str().length(), - cst->get_check_expr_str().ptr()))) { + if (OB_FAIL(databuff_printf(buf, buf_len, pos, ",\n CONSTRAINT "))) { SHARE_SCHEMA_LOG(WARN, "fail to print constraint", K(ret), K(*cst)); + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, new_cst_name, true))) { + SHARE_SCHEMA_LOG(WARN, "fail to print constraint", K(ret)); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, " CHECK (%.*s)", + cst->get_check_expr_str().length(), + cst->get_check_expr_str().ptr()))) { + SHARE_SCHEMA_LOG(WARN, "fail to print constraint", K(ret)); } else if (OB_FAIL(print_constraint_stat(cst->get_rely_flag(), cst->get_enable_flag(), cst->is_validated(), buf, buf_len, pos))) { SHARE_SCHEMA_LOG(WARN, "fail to print constraint stat", K(ret), K(*cst)); @@ -856,11 +878,10 @@ int ObSchemaPrinter::print_fulltext_index_column(const ObTableSchema &table_sche new_col_name, is_oracle_mode))) { SHARE_SCHEMA_LOG(WARN, "fail to generate new name with escape character", K(ret), K(ctxcat_column->get_column_name_str())); + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, new_col_name, is_oracle_mode))) { + SHARE_SCHEMA_LOG(WARN, "fail to print column name", K(ret), K(column)); } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, - is_last && j == ctxcat_ids.count() - 1 - ? (is_oracle_mode ? "\"%s\")" : "`%s`)") - : (is_oracle_mode ? "\"%s\", " : "`%s`, "), - new_col_name.ptr()))) { + is_last && j == ctxcat_ids.count() - 1 ? ")" : ", "))) { SHARE_SCHEMA_LOG(WARN, "fail to print column name", K(ret), K(column)); } else if (OB_FAIL(ctxcat_cols.push_back(ctxcat_column->get_column_name_str()))) { LOG_WARN("get fulltext index column failed", K(ret)); @@ -898,8 +919,9 @@ int ObSchemaPrinter::print_spatial_index_column(const ObTableSchema &table_schem allocator, geo_col->get_column_name_str(), new_col_name, is_oracle_mode))) { SHARE_SCHEMA_LOG(WARN, "fail to generate new name with escape character", K(ret), K(column.get_column_name_str())); - } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, - (is_oracle_mode ? "\"%s\")" : "`%s`)"), new_col_name.ptr()))) { + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, new_col_name, is_oracle_mode))) { + SHARE_SCHEMA_LOG(WARN, "fail to print column name", K(ret), K(new_col_name)); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, ")"))) { SHARE_SCHEMA_LOG(WARN, "fail to print column name", K(ret), K(new_col_name)); } } @@ -967,11 +989,10 @@ int ObSchemaPrinter::print_prefix_index_column(const ObColumnSchemaV2 &column, SHARE_SCHEMA_LOG(WARN, "fail to generate new name with escape character", K(ret), K(column_name)); }else { const_value = (static_cast(t_expr2))->get_value().get_int(); - if (OB_FAIL(databuff_printf(buf, buf_len, pos, - is_last - ? (is_oracle_mode ? "\"%.*s\"(%ld))" : "`%.*s`(%ld))") - : (is_oracle_mode ? "\"%.*s\"(%ld), " : "`%.*s`(%ld), "), - new_col_name.length(), new_col_name.ptr(), const_value))) { + if (OB_FAIL(print_identifier(buf, buf_len, pos, new_col_name, is_oracle_mode))) { + SHARE_SCHEMA_LOG(WARN, "fail to print column name", K(ret)); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, + is_last ? "(%ld))" : "(%ld), ", const_value))) { SHARE_SCHEMA_LOG(WARN, "fail to print column name", K(ret)); } } @@ -1048,11 +1069,9 @@ int ObSchemaPrinter::print_index_column(const ObTableSchema &table_schema, new_col_name, is_oracle_mode))) { SHARE_SCHEMA_LOG(WARN, "fail to generate new name with escape character", K(ret), K(column.get_column_name_str())); - } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, - is_last - ? (is_oracle_mode ? "\"%s\")" : "`%s`)") - : (is_oracle_mode ? "\"%s\", " : "`%s`, "), - new_col_name.ptr()))) { + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, new_col_name, is_oracle_mode))) { + SHARE_SCHEMA_LOG(WARN, "fail to print column name", K(ret), K(column)); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, is_last ? ")" : ", "))) { SHARE_SCHEMA_LOG(WARN, "fail to print column name", K(ret), K(column)); } else { /*do nothing*/ } return ret; @@ -1081,10 +1100,10 @@ int ObSchemaPrinter::print_table_definition_fulltext_indexs( new_col_name, is_oracle_mode))) { SHARE_SCHEMA_LOG(WARN, "fail to generate new name with escape character", K(ret), K(ft_name)); - } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, - is_oracle_mode ? "\"%.*s\", " : "`%.*s`, ", - new_col_name.length(), new_col_name.ptr()))) { + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, new_col_name, is_oracle_mode))) { LOG_WARN("print fulltext column name failed", K(ret), K(ft_name)); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, ", " ))) { + LOG_WARN("print const text failed", K(ret)); } } if (OB_SUCC(ret) && fulltext_indexs.count() > 0) { @@ -1095,9 +1114,10 @@ int ObSchemaPrinter::print_table_definition_fulltext_indexs( new_col_name, is_oracle_mode))) { SHARE_SCHEMA_LOG(WARN, "fail to generate new name with escape character", K(ret), K(ft_name)); - } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, is_oracle_mode ? "\"%.*s\")" : "`%.*s`)", - new_col_name.length(), new_col_name.ptr()))) { + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, new_col_name, is_oracle_mode))) { LOG_WARN("print fulltext column name failed", K(ret), K(ft_name)); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, ")" ))) { + LOG_WARN("print const text failed", K(ret)); } } return ret; @@ -1204,15 +1224,15 @@ int ObSchemaPrinter::print_rowkey_info( K(col->get_column_name_str())); } else if (!col->is_shadow_column()) { if (true == is_first_col) { - if (OB_FAIL(databuff_printf(buf, buf_len, pos, is_oracle_mode ? "\"%s\"" : "`%s`", - new_col_name.ptr()))) { + if (OB_FAIL(print_identifier(buf, buf_len, pos, new_col_name, is_oracle_mode))) { SHARE_SCHEMA_LOG(WARN, "fail to print column name", K(ret), K(col->get_column_name())); } else { is_first_col = false; } } else { - if (OB_FAIL(databuff_printf(buf, buf_len, pos, is_oracle_mode ? ", \"%s\"" : ", `%s`", - new_col_name.ptr()))) { + if (OB_FAIL(databuff_printf(buf, buf_len, pos, ", "))) { + SHARE_SCHEMA_LOG(WARN, "fail to print const ptr", K(ret)); + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, new_col_name, is_oracle_mode))) { SHARE_SCHEMA_LOG(WARN, "fail to print column name", K(ret), K(col->get_column_name())); } } @@ -1251,11 +1271,16 @@ int ObSchemaPrinter::print_referenced_table_info( new_parent_table_name, is_oracle_mode))) { SHARE_SCHEMA_LOG(WARN, "fail to generate new name with escape character", K(ret), K(foreign_key_info->foreign_key_name_)); - } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, - is_oracle_mode ? "\"%s\".\"%s\"(" : "`%s`.`%s`(", - new_parent_db_name.ptr(), new_parent_table_name.ptr()))) { - SHARE_SCHEMA_LOG(WARN, "fail to print database and table name", K(ret), - K(parent_db_schema->get_database_name_str()), K(parent_table_schema->get_table_name_str())); + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, new_parent_db_name, is_oracle_mode))) { + SHARE_SCHEMA_LOG(WARN, "fail to print database name", K(ret), + K(parent_db_schema->get_database_name_str())); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, "."))) { + SHARE_SCHEMA_LOG(WARN, "fail to print const", K(ret)); + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, new_parent_table_name, is_oracle_mode))) { + SHARE_SCHEMA_LOG(WARN, "fail to print table name", K(ret), + K(parent_table_schema->get_table_name_str())); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, "("))) { + SHARE_SCHEMA_LOG(WARN, "fail to print const", K(ret)); } else if (OB_FAIL(print_column_list(*parent_table_schema, foreign_key_info->parent_column_ids_, buf, buf_len, pos))) { LOG_WARN("fail to print_column_list", K(ret), K(parent_table_schema->get_table_name_str())); } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, ") "))) { @@ -1301,11 +1326,11 @@ int ObSchemaPrinter::print_table_definition_foreign_keys(const ObTableSchema &ta new_fk_name, is_oracle_mode))) { SHARE_SCHEMA_LOG(WARN, "fail to generate new name with escape character", K(ret), K(foreign_key_info->foreign_key_name_)); - } else if (!foreign_key_info->foreign_key_name_.empty() && - OB_FAIL(databuff_printf(buf, buf_len, pos, - is_oracle_mode ? "\"%.*s\" " : "`%.*s` ", - new_fk_name.length(), - new_fk_name.ptr()))) { + } else if (!foreign_key_info->foreign_key_name_.empty() && + OB_FAIL(print_identifier(buf, buf_len, pos, new_fk_name, is_oracle_mode))) { + SHARE_SCHEMA_LOG(WARN, "fail to print foreign key name", K(ret)); + } else if (!foreign_key_info->foreign_key_name_.empty() && + OB_FAIL(databuff_printf(buf, buf_len, pos, " "))) { SHARE_SCHEMA_LOG(WARN, "fail to print foreign key name", K(ret)); } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, "FOREIGN KEY ("))) { SHARE_SCHEMA_LOG(WARN, "fail to print FOREIGN KEY(", K(ret)); @@ -1453,15 +1478,15 @@ int ObSchemaPrinter::print_column_list(const T &table_schema, is_oracle_mode))) { SHARE_SCHEMA_LOG(WARN, "fail to generate new name with escape character", K(ret), K(ori_col_name)); } else if (true == is_first_col) { - if (OB_FAIL(databuff_printf(buf, buf_len, pos, is_oracle_mode ? "\"%s\"" : "`%s`", - new_col_name.ptr()))) { + if (OB_FAIL(print_identifier(buf, buf_len, pos, new_col_name, is_oracle_mode))) { SHARE_SCHEMA_LOG(WARN, "fail to print child column name", K(ret), K(ori_col_name)); } else { is_first_col = false; } } else { - if (OB_FAIL(databuff_printf(buf, buf_len, pos,is_oracle_mode ? ", \"%s\"" : ", `%s`", - new_col_name.ptr()))) { + if (OB_FAIL(databuff_printf(buf, buf_len, pos, ", "))) { + SHARE_SCHEMA_LOG(WARN, "fail to print column name", K(ret), K(ori_col_name)); + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, new_col_name, is_oracle_mode))) { SHARE_SCHEMA_LOG(WARN, "fail to print column name", K(ret), K(ori_col_name)); } } @@ -2334,9 +2359,9 @@ int ObSchemaPrinter::print_index_definition_columns( OB_LOG(WARN, "expr type is not int", K(ret)); } else { const_value = (static_cast(t_expr2))->get_value().get_int(); - if (OB_FAIL(databuff_printf(buf, buf_len, pos, - !is_oracle_mode ? "`%.*s`(%ld)" : "\"%.*s\"(%ld)", - column_name.length(), column_name.ptr(), const_value))) { + if (OB_FAIL(print_identifier(buf, buf_len, pos, column_name, is_oracle_mode))) { + OB_LOG(WARN, "fail to print column name", K(ret), K(*col)); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, "(%ld)", const_value))) { OB_LOG(WARN, "fail to print column name", K(ret), K(*col)); } else if (is_agent_mode && OB_FAIL(databuff_printf(buf, buf_len, pos, @@ -2349,9 +2374,9 @@ int ObSchemaPrinter::print_index_definition_columns( } } else { // 普通索引 - if (OB_FAIL(databuff_printf(buf, buf_len, pos, - !is_oracle_mode ? " `%s`" : " \"%s\"", - col->get_column_name()))) { + if (OB_FAIL(databuff_printf(buf, buf_len, pos, " "))) { + OB_LOG(WARN, "fail to print column name", K(ret), K(*col)); + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, col->get_column_name(), is_oracle_mode))) { OB_LOG(WARN, "fail to print column name", K(ret), K(*col)); } else if (is_agent_mode && OB_FAIL(databuff_printf(buf, buf_len, pos, " id %lu", @@ -2394,9 +2419,9 @@ int ObSchemaPrinter::print_full_text_columns_definition(const ObTableSchema &dat OB_LOG(WARN, "column id is invalid", K(ret), K(column_id)); } else if (OB_FAIL(full_text_columns.push_back(data_col->get_column_name()))) { OB_LOG(WARN, "failed to push back column names", K(ret)); - } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, "%s `%s`", - is_first ? "" : ",\n", - data_col->get_column_name()))) { + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, "%s ", is_first ? "" : ",\n"))) { + OB_LOG(WARN, "fail to print column name", K(ret), K(*data_col)); + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, data_col->get_column_name(), false))) { OB_LOG(WARN, "fail to print column name", K(ret), K(*data_col)); } else if (is_agent_mode && OB_FAIL(databuff_printf(buf, buf_len, pos, " id %lu", @@ -2450,10 +2475,10 @@ int ObSchemaPrinter::print_table_index_stroing( first_storing_column = false; } if (OB_SUCC(ret)) { - if (OB_FAIL(databuff_printf(buf, buf_len, pos, !is_oracle_mode ? "`%s`, " : "\"%s\", ", - (*row_col)->get_column_name()))) { - OB_LOG(WARN, "fail to print column name", K(ret), - K((*row_col)->get_column_name())); + if (OB_FAIL(print_identifier(buf, buf_len, pos, (*row_col)->get_column_name(), is_oracle_mode))) { + OB_LOG(WARN, "fail to print column name", K(ret), K((*row_col)->get_column_name())); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, ", "))) { + OB_LOG(WARN, "fail to print const", K(ret)); } } } @@ -2521,26 +2546,41 @@ int ObSchemaPrinter::print_index_table_definition( LOG_WARN("fail to check oracle mode", KR(ret), KPC(index_table_schema)); } else if (index_table_schema->is_unique_index()) { if (OB_FAIL(databuff_printf(buf, buf_len, pos, - !is_oracle_mode ? "CREATE UNIQUE INDEX if not exists `%s`.`%.*s` on `%s`.`%s` (\n" - : "CREATE UNIQUE INDEX \"%s\".\"%.*s\" on \"%s\".\"%s\" (\n", - ds_schema->get_database_name(), index_name.length(), index_name.ptr(), ds_schema->get_database_name(), table_schema->get_table_name()))) { + !is_oracle_mode ? "CREATE UNIQUE INDEX if not exists " + : "CREATE UNIQUE INDEX "))) { OB_LOG(WARN, "fail to print create table prefix", K(ret), K(table_schema->get_table_name())); } } else if (index_table_schema->is_domain_index()) { if (OB_FAIL(databuff_printf(buf, buf_len, pos, - !is_oracle_mode ? "CREATE FULLTEXT INDEX if not exists `%s`.`%.*s` on `%s`.`%s` (\n" - : "CREATE FULLTEXT INDEX \"%s\".\"%.*s\" on \"%s\".\"%s\" (\n", - ds_schema->get_database_name(), index_name.length(), index_name.ptr(), ds_schema->get_database_name(), table_schema->get_table_name()))) { + !is_oracle_mode ? "CREATE FULLTEXT INDEX if not exists " + : "CREATE FULLTEXT INDEX "))) { OB_LOG(WARN, "fail to print create table prefix", K(ret), K(table_schema->get_table_name())); } } else { if (OB_FAIL(databuff_printf(buf, buf_len, pos, - !is_oracle_mode ? "CREATE INDEX if not exists `%s`.`%.*s` on `%s`.`%s` (\n" - : "CREATE INDEX \"%s\".\"%.*s\" on \"%s\".\"%s\" (\n", - ds_schema->get_database_name(), index_name.length(), index_name.ptr(), ds_schema->get_database_name(), table_schema->get_table_name()))) { + !is_oracle_mode ? "CREATE INDEX if not exists " + : "CREATE INDEX "))) { OB_LOG(WARN, "fail to print create table prefix", K(ret), K(table_schema->get_table_name())); } } + if (OB_FAIL(ret)) { + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, ds_schema->get_database_name(), is_oracle_mode))) { + OB_LOG(WARN, "fail to print create table prefix", K(ret), K(table_schema->get_table_name())); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, "."))) { + OB_LOG(WARN, "fail to print const str", K(ret)); + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, index_name, is_oracle_mode))) { + OB_LOG(WARN, "fail to print create table prefix", K(ret), K(table_schema->get_table_name())); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, " on "))) { + OB_LOG(WARN, "fail to print const str", K(ret)); + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, ds_schema->get_database_name(), is_oracle_mode))) { + OB_LOG(WARN, "fail to print create table prefix", K(ret), K(table_schema->get_table_name())); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, "."))) { + OB_LOG(WARN, "fail to print const str", K(ret)); + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, table_schema->get_table_name(), is_oracle_mode))) { + OB_LOG(WARN, "fail to print create table prefix", K(ret), K(table_schema->get_table_name())); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, " (\n"))) { + OB_LOG(WARN, "fail to print const str", K(ret)); + } } if (OB_SUCC(ret)) { @@ -2586,13 +2626,16 @@ int ObSchemaPrinter::print_view_definiton( SHARE_SCHEMA_LOG(WARN, "Unknow table", K(ret), K(table_id)); } else if (OB_FAIL(table_schema->check_if_oracle_compat_mode(is_oracle_mode))) { LOG_WARN("fail to check oracle mode", KR(ret), KPC(table_schema)); - } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, - is_oracle_mode ? "CREATE %s%sVIEW \"%s\" AS %.*s" : "CREATE %s%sVIEW `%s` AS %.*s", + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, "CREATE %s%sVIEW ", is_oracle_mode && table_schema->is_view_created_by_or_replace_force() ? "OR REPLACE FORCE " : "", - table_schema->is_materialized_view() ? "MATERIALIZED " : "", - table_schema->get_table_name(), - table_schema->get_view_schema().get_view_definition_str().length(), - table_schema->get_view_schema().get_view_definition_str().ptr()))) { + table_schema->is_materialized_view() ? "MATERIALIZED " : ""))) { + SHARE_SCHEMA_LOG(WARN, "fail to print view definition", K(ret)); + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, table_schema->get_table_name(), is_oracle_mode))) { + SHARE_SCHEMA_LOG(WARN, "fail to print view definition", K(ret)); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, " AS "))) { + SHARE_SCHEMA_LOG(WARN, "fail to print view definition", K(ret)); + } else if (OB_FAIL(print_view_define_str(buf, buf_len, pos, is_oracle_mode, + table_schema->get_view_schema().get_view_definition_str()))) { SHARE_SCHEMA_LOG(WARN, "fail to print view definition", K(ret)); } else if (!table_schema->get_view_schema().get_view_is_updatable() && is_oracle_mode) { // with read only is only supported in syntax in oracle mode, but some inner system view is @@ -2638,10 +2681,12 @@ int ObSchemaPrinter::print_tablegroup_definition( } else if (OB_FAIL(tablegroup_schema->check_if_oracle_compat_mode(is_oracle_mode))) { SHARE_SCHEMA_LOG(WARN, "fail to check oracle mode", KR(ret), K(tablegroup_id)); } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, - is_oracle_mode - ? "CREATE TABLEGROUP \"%s\" " - : "CREATE TABLEGROUP IF NOT EXISTS `%s` ", - tablegroup_schema->get_tablegroup_name().ptr()))) { + is_oracle_mode ? "CREATE TABLEGROUP " + : "CREATE TABLEGROUP IF NOT EXISTS "))) { + SHARE_SCHEMA_LOG(WARN, "fail to print create tablegroup prefix", K(ret), K(tablegroup_schema->get_tablegroup_name())); + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, tablegroup_schema->get_tablegroup_name(), is_oracle_mode))) { + SHARE_SCHEMA_LOG(WARN, "fail to print create tablegroup prefix", K(ret), K(tablegroup_schema->get_tablegroup_name())); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, " "))) { SHARE_SCHEMA_LOG(WARN, "fail to print create tablegroup prefix", K(ret), K(tablegroup_schema->get_tablegroup_name())); } else if (OB_FAIL(print_tablegroup_definition_tablegroup_options(*tablegroup_schema, buf, buf_len, pos, agent_mode))) { SHARE_SCHEMA_LOG(WARN, "fail to print tablegroup options", K(ret), K(*tablegroup_schema)); @@ -2659,8 +2704,11 @@ int ObSchemaPrinter::print_tablespace_definition( const ObTablespaceSchema *tablespace_schema = NULL; if (OB_FAIL(schema_guard_.get_tablespace_schema(tenant_id, tablespace_id, tablespace_schema))) { SHARE_SCHEMA_LOG(WARN, "fail to get tablespace schema", K(ret)); - } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, - lib::is_oracle_mode() ? "CREATE TABLESPACE \"%s\" " : "CREATE TABLESPACE `%s` ", tablespace_schema->get_tablespace_name().ptr()))) { + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, "CREATE TABLESPACE "))) { + SHARE_SCHEMA_LOG(WARN, "fail to print create tablespace prefix", K(ret), K(tablespace_schema->get_tablespace_name())); + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, tablespace_schema->get_tablespace_name(), lib::is_oracle_mode()))) { + SHARE_SCHEMA_LOG(WARN, "fail to print create tablespace prefix", K(ret), K(tablespace_schema->get_tablespace_name())); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, " "))) { SHARE_SCHEMA_LOG(WARN, "fail to print create tablespace prefix", K(ret), K(tablespace_schema->get_tablespace_name())); } return ret; @@ -2677,9 +2725,11 @@ int ObSchemaPrinter::print_tablespace_definition_for_table( /*do nothing*/ } else if (OB_FAIL(schema_guard_.get_tablespace_schema(tenant_id, tablespace_id, tablespace_schema))) { SHARE_SCHEMA_LOG(WARN, "fail to get tablespace schema", K(ret)); - } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, - lib::is_oracle_mode() ? " TABLESPACE \"%s\" " : " TABLESPACE `%s` ", - tablespace_schema->get_tablespace_name().ptr()))) { + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, " TABLESPACE "))) { + SHARE_SCHEMA_LOG(WARN, "fail to print create tablespace prefix", K(ret), K(tablespace_schema->get_tablespace_name())); + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, tablespace_schema->get_tablespace_name(), lib::is_oracle_mode()))) { + SHARE_SCHEMA_LOG(WARN, "fail to print create tablespace prefix", K(ret), K(tablespace_schema->get_tablespace_name())); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, " "))) { SHARE_SCHEMA_LOG(WARN, "fail to print create tablespace prefix", K(ret), K(tablespace_schema->get_tablespace_name())); } return ret; @@ -3139,10 +3189,12 @@ int ObSchemaPrinter::print_database_definiton( ret = OB_ERR_BAD_DATABASE; SHARE_SCHEMA_LOG(WARN, "Unknow database", K(ret), K(tenant_id), K(database_id)); } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, - lib::is_oracle_mode() ? "CREATE DATABASE %s\"%.*s\"" : "CREATE DATABASE %s`%.*s`", - true == if_not_exists? "IF NOT EXISTS " : "", - database_schema->get_database_name_str().length(), - database_schema->get_database_name_str().ptr()))) { + "CREATE DATABASE %s", + true == if_not_exists? "IF NOT EXISTS " : ""))) { + SHARE_SCHEMA_LOG(WARN, "fail to print database definition", K(ret)); + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, + database_schema->get_database_name_str(), + lib::is_oracle_mode()))) { SHARE_SCHEMA_LOG(WARN, "fail to print database definition", K(ret)); } } @@ -3812,10 +3864,11 @@ int ObSchemaPrinter::print_routine_definition_param(const ObRoutineInfo &routine } if (OB_SUCC(ret)) { - OZ (databuff_printf(buf, buf_len, pos, - is_oracle_mode ? " \"%.*s\"" : " `%.*s`", - param->get_param_name().length(), - param->get_param_name().ptr())); + if (OB_FAIL(databuff_printf(buf, buf_len, pos, " "))) { + LOG_WARN("failed to print const str", K(ret)); + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, param->get_param_name(), is_oracle_mode))) { + LOG_WARN("failed to print param", K(ret)); + } } if (OB_SUCC(ret) && is_oracle_mode) { @@ -3880,12 +3933,12 @@ int ObSchemaPrinter::print_routine_definition(const ObRoutineInfo *routine_info, routine_info->get_priv_user().length(), routine_info->get_priv_user().ptr(), routine_type)); - OZ (databuff_printf(buf, buf_len, pos, - lib::is_oracle_mode() ? "\"%.*s\".\"%.*s\"\n" : "`%.*s`.`%.*s`\n", - db_schema->get_database_name_str().length(), - db_schema->get_database_name_str().ptr(), - routine_info->get_routine_name().length(), - routine_info->get_routine_name().ptr()), K(routine_type), K(db_name), K(routine_info)); + OZ (print_identifier(buf, buf_len, pos, db_schema->get_database_name_str(), lib::is_oracle_mode()), + K(routine_type), K(db_name), K(routine_info)); + OZ (databuff_printf(buf, buf_len, pos, ".")); + OZ (print_identifier(buf, buf_len, pos, routine_info->get_routine_name(), lib::is_oracle_mode()), + K(routine_type), K(db_name), K(routine_info)); + OZ (databuff_printf(buf, buf_len, pos, "\n")); if (OB_SUCC(ret) && routine_info->get_param_count() > 0) { OZ (databuff_printf(buf, buf_len, pos, "(\n")); OZ (print_routine_definition_param(*routine_info, param_list, buf, buf_len, pos, tz_info)); @@ -3914,10 +3967,9 @@ int ObSchemaPrinter::print_routine_definition(const ObRoutineInfo *routine_info, OZ (databuff_printf(buf, buf_len, pos, "%s", routine_info->is_invoker_right() ? "\nINVOKER" : "")); if (OB_SUCC(ret) && OB_NOT_NULL(routine_info->get_comment())) { - OZ (databuff_printf(buf, buf_len, pos, "\n%s%.*s%s\n","COMMENT `", - routine_info->get_comment().length(), - routine_info->get_comment().ptr(), - "`")); + OZ (databuff_printf(buf, buf_len, pos, "\nCOMMENT ")); + OZ (print_identifier(buf, buf_len, pos, routine_info->get_comment(), false)); + OZ (databuff_printf(buf, buf_len, pos, "\n")); } } if (OB_SUCC(ret) && lib::is_oracle_mode() && !clause.empty()) { @@ -4073,22 +4125,25 @@ int ObSchemaPrinter::print_foreign_key_definition( OV (!child_table_name.empty()); OV (!parent_table_name.empty()); - OX (BUF_PRINTF(is_oracle_mode ? - "ALTER TABLE \"%.*s\".\"%.*s\" ADD CONSTRAINT " : - "ALTER TABLE `%.*s`.`%.*s` ADD CONSTRAINT ", - child_database_name.length(), child_database_name.ptr(), - child_table_name.length(), child_table_name.ptr())); + OX (BUF_PRINTF("ALTER TABLE ")); + OZ (print_identifier(buf, buf_len, pos, child_database_name, is_oracle_mode)); + OX (BUF_PRINTF(".")); + OZ (print_identifier(buf, buf_len, pos, child_table_name, is_oracle_mode)); + OX (BUF_PRINTF(" ADD CONSTRAINT ")); + if (!foreign_key_name.empty()) { - OX (BUF_PRINTF(is_oracle_mode ? "\"%.*s\" " : "`%.*s` ", - foreign_key_name.length(), foreign_key_name.ptr())); + OZ (print_identifier(buf, buf_len, pos, foreign_key_name, is_oracle_mode)); + OX (BUF_PRINTF(" ")); } OX (BUF_PRINTF("FOREIGN KEY (")); OX (print_column_list(*child_table_schema, foreign_key_info.child_column_ids_, buf, buf_len, pos)); - OX (BUF_PRINTF(is_oracle_mode ? - ") REFERENCES \"%.*s\".\"%.*s\" (" : - ") REFERENCES `%.*s`.`%.*s` (", - parent_database_name.length(), parent_database_name.ptr(), - parent_table_name.length(), parent_table_name.ptr())); + + OX (BUF_PRINTF(") REFERENCES ")); + OZ (print_identifier(buf, buf_len, pos, parent_database_name, is_oracle_mode)); + OX (BUF_PRINTF(".")); + OZ (print_identifier(buf, buf_len, pos, parent_table_name, is_oracle_mode)); + OX (BUF_PRINTF(" (")); + if (OB_SUCC(ret)) { if (foreign_key_info.is_parent_table_mock_) { OX (print_column_list(*mock_fk_parent_table_schema, foreign_key_info.parent_column_ids_, buf, buf_len, pos)); @@ -4517,12 +4572,13 @@ int ObSchemaPrinter::print_constraint_definition(const ObDatabaseSchema &db_sche } } else { const ObString &cst_expr = cst->get_check_expr_str(); - OX (BUF_PRINTF(is_oracle_mode ? - "ALTER TABLE \"%.*s\".\"%.*s\" ADD CONSTRAINT \"%.*s\" " : - "ALTER TABLE `%.*s`.`%.*s` ADD CONSTRAINT `%.*s` ", - db_name.length(), db_name.ptr(), - tb_name.length(), tb_name.ptr(), - cst_name.length(), cst_name.ptr())); + OX (BUF_PRINTF("ALTER TABLE ")); + OZ (print_identifier(buf, buf_len, pos, db_name, is_oracle_mode)); + OX (BUF_PRINTF(".")); + OZ (print_identifier(buf, buf_len, pos, tb_name, is_oracle_mode)); + OX (BUF_PRINTF(" ADD CONSTRAINT ")); + OZ (print_identifier(buf, buf_len, pos, cst_name, is_oracle_mode)); + OX (BUF_PRINTF(" ")); switch (cst->get_constraint_type()) { case CONSTRAINT_TYPE_PRIMARY_KEY: OX (BUF_PRINTF("PRIMARY KEY (")); @@ -4563,30 +4619,22 @@ int ObSchemaPrinter::print_user_definition(uint64_t tenant_id, const ObString &host_name = user_info.get_host_name_str(); const ObString &user_passwd = user_info.get_passwd_str(); bool is_oracle_mode = lib::is_oracle_mode(); - - if (!is_role) { - if (OB_FAIL(databuff_printf(buf, buf_len, pos, - is_oracle_mode ? "create user \"%.*s\" " : "create user if not exists `%.*s` ", - user_name.length(), user_name.ptr()))) { - SHARE_SCHEMA_LOG(WARN, "fail to print user name", K(user_name), K(ret)); - } else if (host_name.compare(OB_DEFAULT_HOST_NAME) != 0) { - if (OB_FAIL(databuff_printf(buf, buf_len, pos, - is_oracle_mode ? "@\"%.*s\" " : "@`%.*s` ", - host_name.length(), host_name.ptr()))) { - SHARE_SCHEMA_LOG(WARN, "fail to print host name", K(host_name), K(ret)); - } - } - } else { - if (OB_FAIL(databuff_printf(buf, buf_len, pos, - is_oracle_mode ? "create role \"%.*s\" " : "create role if not exists `%.*s` ", - user_name.length(), user_name.ptr()))) { - SHARE_SCHEMA_LOG(WARN, "fail to print user name", K(user_name), K(ret)); - } else if (host_name.compare(OB_DEFAULT_HOST_NAME) != 0) { - if (OB_FAIL(databuff_printf(buf, buf_len, pos, - is_oracle_mode ? "@\"%.*s\" " : "@`%.*s` ", - host_name.length(), host_name.ptr()))) { - SHARE_SCHEMA_LOG(WARN, "fail to print host name", K(host_name), K(ret)); - } + if (OB_FAIL(databuff_printf(buf, buf_len, pos, + is_oracle_mode ? "create %s " + : "create %s if not exists ", + is_role ? "role" : "user"))) { + SHARE_SCHEMA_LOG(WARN, "fail to print user name", K(user_name), K(ret)); + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, user_name, is_oracle_mode))) { + SHARE_SCHEMA_LOG(WARN, "fail to print user name", K(user_name), K(ret)); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, " "))) { + SHARE_SCHEMA_LOG(WARN, "fail to print user name", K(user_name), K(ret)); + } else if (host_name.compare(OB_DEFAULT_HOST_NAME) != 0) { + if (OB_FAIL(databuff_printf(buf, buf_len, pos, "@"))) { + SHARE_SCHEMA_LOG(WARN, "fail to print host name", K(host_name), K(ret)); + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, host_name, is_oracle_mode))) { + SHARE_SCHEMA_LOG(WARN, "fail to print host name", K(host_name), K(ret)); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, " "))) { + SHARE_SCHEMA_LOG(WARN, "fail to print host name", K(host_name), K(ret)); } } @@ -4609,10 +4657,13 @@ int ObSchemaPrinter::print_user_definition(uint64_t tenant_id, SHARE_SCHEMA_LOG(WARN, "get profile schena failed", K(ret)); } } else { - if (OB_FAIL(databuff_printf(buf, buf_len, pos, - is_oracle_mode ? "PROFILE \"%.*s\" " : "PROFILE `%.*s` ", - profile_schema->get_profile_name_str().length(), - profile_schema->get_profile_name_str().ptr()))) { + if (OB_FAIL(databuff_printf(buf, buf_len, pos, "PROFILE "))) { + SHARE_SCHEMA_LOG(WARN, "fail to print profile", K(ret)); + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, + profile_schema->get_profile_name_str(), + is_oracle_mode))) { + SHARE_SCHEMA_LOG(WARN, "fail to print profile", K(ret)); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, " "))) { SHARE_SCHEMA_LOG(WARN, "fail to print profile", K(ret)); } } @@ -4863,6 +4914,222 @@ int ObSchemaPrinter::print_external_table_file_info(const ObTableSchema &table_s return ret; } +int ObSchemaPrinter::print_identifier(char* buf, + const int64_t& buf_len, + int64_t& pos, + const ObString &ident, + bool is_oracle_mode) const +{ + int ret = OB_SUCCESS; + bool require_quotes = false; + const char* format_str = "%.*s"; + if (is_oracle_mode) { + format_str = "\"%.*s\""; + require_quotes = true; + } else if (sql_quote_show_create_) { + format_str = "`%.*s`"; + require_quotes = true; + } else if (OB_FAIL(ObSQLUtils::print_identifier_require_quotes(CS_TYPE_UTF8MB4_GENERAL_CI, + ident, + require_quotes))) { + LOG_WARN("failed to check identifier require quotes", K(ret)); + } else if (!require_quotes) { + format_str = "%.*s"; + } else { + format_str = "`%.*s`"; + } + + if (OB_FAIL(ret)) { + } else if (!require_quotes || (require_quotes && !ansi_quotes_)) { + if (OB_FAIL(databuff_printf(buf, buf_len, pos, format_str, ident.length(), + ident.empty() ? "" : ident.ptr()))){ + SHARE_SCHEMA_LOG(WARN, "fail to print indentifer", K(ret)); + } + } else { + if (OB_FAIL(databuff_printf(buf, buf_len, pos, "\""))) { + SHARE_SCHEMA_LOG(WARN, "fail to print indentifer", K(ret)); + } + for (int64_t index = 0; OB_SUCC(ret) && !ident.empty() && index < ident.length(); ++index) { + if (ident[index] == '"') { + if (OB_FAIL(databuff_printf(buf, buf_len, pos, "\"\""))) { + SHARE_SCHEMA_LOG(WARN, "fail to print indentifer", K(ret)); + } + } else if (ident[index] == '`' && + index + 1 < ident.length() && + ident[index + 1] == '`') { + if (OB_FAIL(databuff_printf(buf, buf_len, pos, "`"))) { + SHARE_SCHEMA_LOG(WARN, "fail to print indentifer", K(ret)); + } else { + ++index; + } + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, "%c", ident[index]))) { + SHARE_SCHEMA_LOG(WARN, "fail to print indentifer", K(ret)); + } + } + if (OB_SUCC(ret) && OB_FAIL(databuff_printf(buf, buf_len, pos, "\""))) { + SHARE_SCHEMA_LOG(WARN, "fail to print indentifer", K(ret)); + } + } + return ret; +} + +int ObSchemaPrinter::print_view_define_str(char* buf, + const int64_t &buf_len, + int64_t& pos, + bool is_oracle_mode, + const ObString &sql) const +{ + int ret = OB_SUCCESS; + if (is_oracle_mode || + (sql_quote_show_create_ && !ansi_quotes_)) { + if (OB_FAIL(databuff_printf(buf, buf_len, pos, "%.*s", sql.length(), sql.ptr()))) { + SHARE_SCHEMA_LOG(WARN, "fail to print view define str", K(ret)); + } + } else { + const char *begin = sql.ptr(); + const char *cursor = begin; + const char *end = begin + sql.length(); + int state = 0; + while (OB_SUCC(ret) && cursor < end) { + if (0 == state) { + // init state, find `, ', " ,/* ,-- + while (cursor < end) { + if (*cursor == '`' || *cursor == '\'' || *cursor == '"') { + break; + } else if (*cursor == '/' && cursor + 1 < end && *(cursor + 1) == '*') { + break; + } else if (*cursor == '-' && cursor + 1 < end && *(cursor + 1) == '-') { + break; + } else { + ++cursor; + } + } + if (cursor - begin > 0 && + OB_FAIL(databuff_printf(buf, buf_len, pos, + "%.*s", + static_cast(cursor - begin), + begin))) { + SHARE_SCHEMA_LOG(WARN, "fail to print view define str", K(ret)); + } else if (OB_FALSE_IT(begin = cursor)) { + } else if (cursor >= end) { + // do nothing + } else if (*cursor == '`') { + state = 1; + } else if (*cursor == '\'') { + state = 2; + } else if (*cursor == '"') { + state = 3; + } else if (*cursor == '/') { + state = 4; + } else if (*cursor == '-') { + state = 5; + } else { + ret = OB_ERR_UNEXPECTED; + SHARE_SCHEMA_LOG(WARN, "fail to print view define str, get unexpected state", K(ret), K(cursor)); + } + } else if (1 == state) { + // process ` + ++cursor; + while (cursor < end) { + if (*cursor == '`' && cursor + 1 < end && *(cursor + 1) == '`') { + cursor += 2; + } else if (*cursor == '`') { + break; + } else { + ++cursor; + } + } + if (OB_UNLIKELY(cursor >= end) || + OB_UNLIKELY(*cursor != '`') || + OB_UNLIKELY(cursor - begin < 1)) { + ret = OB_ERR_UNEXPECTED; + SHARE_SCHEMA_LOG(WARN, "fail to print view define str", K(ret), K(sql), K(begin), K(cursor)); + } else if (OB_FAIL(print_identifier(buf, buf_len, pos, + ObString(cursor - begin - 1, begin + 1), + is_oracle_mode))) { + SHARE_SCHEMA_LOG(WARN, "fail to print view define str", K(ret), K(sql), K(begin)); + } else { + ++cursor; + begin = cursor; + state = 0; + } + } else if (2 == state || 3 == state) { + // process ' " + ++cursor; + const char c = (2 == state) ? '\'' : '"'; + while (cursor < end) { + if (*cursor == '\\' && cursor + 1 < end && *(cursor + 1) == c) { + cursor += 2; + } else if (*cursor == c) { + break; + } else { + ++cursor; + } + } + if (OB_UNLIKELY(cursor >= end) || + OB_UNLIKELY(*cursor != c) || + OB_UNLIKELY(cursor - begin < 1)) { + ret = OB_ERR_UNEXPECTED; + SHARE_SCHEMA_LOG(WARN, "fail to print view define str", K(ret), K(sql), K(begin), K(cursor)); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, + "%.*s", static_cast(cursor - begin + 1), + begin))) { + SHARE_SCHEMA_LOG(WARN, "fail to print view define str", K(ret), K(sql), K(begin), K(cursor)); + } else { + ++cursor; + begin = cursor; + state = 0; + } + } else if (4 == state) { + // process /* + cursor += 2; + while (cursor < end) { + if (*cursor == '*' && cursor + 1 < end && *(cursor + 1) == '/') { + break; + } else { + ++cursor; + } + } + if (OB_UNLIKELY(cursor + 1 >= end) || + OB_UNLIKELY(*cursor != '*' || *(cursor + 1) != '/')) { + ret = OB_ERR_UNEXPECTED; + SHARE_SCHEMA_LOG(WARN, "fail to print view define str", K(ret), K(sql), K(begin), K(cursor)); + } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, + "%.*s", static_cast(cursor - begin + 2), + begin))) { + SHARE_SCHEMA_LOG(WARN, "fail to print view define str", K(ret), K(sql), K(begin), K(cursor)); + } else { + cursor += 2; + begin = cursor; + state = 0; + } + } else if (5 == state) { + // process -- + cursor += 2; + while (cursor < end) { + if (*cursor == '\n') { + ++cursor; + break; + } else { + ++cursor; + } + } + if (OB_FAIL(databuff_printf(buf, buf_len, pos, + "%.*s", static_cast(cursor - begin), + begin))) { + SHARE_SCHEMA_LOG(WARN, "fail to print view define str", K(ret), K(sql), K(begin), K(cursor)); + } else { + begin = cursor; + state = 0; + } + } else { + ret = OB_ERR_UNEXPECTED; + SHARE_SCHEMA_LOG(WARN, "fail to print view define str, get unexpected state", K(ret), K(cursor), K(state)); + } + } + } + return ret; +} } // end namespace schema } //end of namespace share diff --git a/src/share/schema/ob_schema_printer.h b/src/share/schema/ob_schema_printer.h index 1f63ec87d3..4d79620c7b 100644 --- a/src/share/schema/ob_schema_printer.h +++ b/src/share/schema/ob_schema_printer.h @@ -55,7 +55,10 @@ class ObConstraint; class ObSchemaPrinter { public: - explicit ObSchemaPrinter(ObSchemaGetterGuard &schema_guard, bool strict_compat = false); + explicit ObSchemaPrinter(ObSchemaGetterGuard &schema_guard, + bool strict_compat = false, + bool sql_quote_show_create = true, + bool ansi_quotes = false); virtual ~ObSchemaPrinter() { } private: ObSchemaPrinter(); @@ -447,6 +450,17 @@ public: char* buf, const int64_t& buf_len, int64_t& pos) const; + int print_identifier(char* buf, + const int64_t& buf_len, + int64_t& pos, + const ObString &ident, + bool is_oracle_mode) const; + + int print_view_define_str(char* buf, + const int64_t &buf_len, + int64_t& pos, + bool is_oracle_mode, + const ObString &sql) const; private: static bool is_subpartition_valid_in_mysql(const ObTableSchema &table_schema) { @@ -459,6 +473,8 @@ private: ObSchemaGetterGuard &schema_guard_; bool strict_compat_; + bool sql_quote_show_create_; + bool ansi_quotes_; }; diff --git a/src/sql/ob_dml_stmt_printer.cpp b/src/sql/ob_dml_stmt_printer.cpp index 2a83aa0451..9da36e02d1 100644 --- a/src/sql/ob_dml_stmt_printer.cpp +++ b/src/sql/ob_dml_stmt_printer.cpp @@ -162,9 +162,8 @@ int ObDMLStmtPrinter::print_table_with_subquery(const TableItem *table_item) subquery_print_params))) { LOG_WARN("failed to print subquery", K(ret)); } else if (!table_item->alias_name_.empty()) { - PRINT_QUOT_WITH_SPACE; - DATA_PRINTF("%.*s", LEN_AND_PTR(table_item->alias_name_)); - PRINT_QUOT; + DATA_PRINTF(" "); + PRINT_IDENT_WITH_QUOT(table_item->alias_name_); } else { DATA_PRINTF(" "); PRINT_TABLE_NAME(print_params_, table_item); @@ -211,9 +210,8 @@ int ObDMLStmtPrinter::print_table(const TableItem *table_item, LOG_WARN("failed to print base table", K(ret), K(*table_item)); //table in insert all can't print alias(bug: } else if (!no_print_alias) { - PRINT_QUOT_WITH_SPACE; - DATA_PRINTF("%.*s", LEN_AND_PTR(table_item->alias_name_)); - PRINT_QUOT; + DATA_PRINTF(" "); + PRINT_IDENT_WITH_QUOT(table_item->alias_name_); } break; } @@ -329,19 +327,16 @@ int ObDMLStmtPrinter::print_table(const TableItem *table_item, case TableItem::CTE_TABLE: { PRINT_TABLE_NAME(print_params_, table_item); if (! table_item->alias_name_.empty()) { - PRINT_QUOT_WITH_SPACE; - DATA_PRINTF("%.*s", LEN_AND_PTR(table_item->alias_name_)); - PRINT_QUOT; + DATA_PRINTF(" "); + PRINT_IDENT_WITH_QUOT(table_item->alias_name_); } break; } case TableItem::FUNCTION_TABLE: { DATA_PRINTF("TABLE("); OZ (expr_printer_.do_print(table_item->function_table_expr_, T_FROM_SCOPE)); - DATA_PRINTF(")"); - PRINT_QUOT_WITH_SPACE; - DATA_PRINTF("%.*s", LEN_AND_PTR(table_item->alias_name_)); - PRINT_QUOT; + DATA_PRINTF(") "); + PRINT_IDENT_WITH_QUOT(table_item->alias_name_); break; } case TableItem::JSON_TABLE: { @@ -357,9 +352,8 @@ int ObDMLStmtPrinter::print_table(const TableItem *table_item, if (!print_params_.for_dblink_) { PRINT_TABLE_NAME(print_params_, table_item); if (!table_item->alias_name_.empty()) { - PRINT_QUOT_WITH_SPACE; - DATA_PRINTF("%.*s", LEN_AND_PTR(table_item->alias_name_)); - PRINT_QUOT; + DATA_PRINTF(" "); + PRINT_IDENT_WITH_QUOT(table_item->alias_name_); } } else if (OB_FAIL(print_table_with_subquery(table_item))) { LOG_WARN("failed to print table with subquery", K(ret)); @@ -1454,9 +1448,7 @@ int ObDMLStmtPrinter::print_cte_define_title(TableItem* cte_table) || T_CTE_CYCLE_COLUMN == sub_select_items.at(i).expr_->get_expr_type()) { //伪列不需要打印出来 } else { - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(sub_select_items.at(i).alias_name_)); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(sub_select_items.at(i).alias_name_); if (i != sub_select_items.count() - 1 && T_CTE_SEARCH_COLUMN != sub_select_items.at(i+1).expr_->get_expr_type() && T_CTE_CYCLE_COLUMN != sub_select_items.at(i+1).expr_->get_expr_type()) { @@ -1522,7 +1514,6 @@ int ObDMLStmtPrinter::print_search_and_cycle(const ObSelectStmt *sub_select_stmt for (int64_t i = 0; i < search_items.count() && OB_SUCC(ret); ++i) { allocator.reuse(); ObString column_name = ((ObColumnRefRawExpr*)(search_items.at(i).expr_))->get_column_name(); - CONVERT_CHARSET_FOR_RPINT(allocator, column_name); PRINT_COLUMN_NAME(column_name); if (lib::is_mysql_mode()) { if (is_descending_direction(search_items.at(i).order_type_)) { @@ -1559,7 +1550,6 @@ int ObDMLStmtPrinter::print_search_and_cycle(const ObSelectStmt *sub_select_stmt for (int64_t i = 0; i < cycle_items.count() && OB_SUCC(ret); ++i) { allocator.reuse(); ObString column_name = ((ObColumnRefRawExpr*)(cycle_items.at(i).expr_))->get_column_name(); - CONVERT_CHARSET_FOR_RPINT(allocator, column_name); PRINT_COLUMN_NAME(column_name); if (i != cycle_items.count() - 1) { DATA_PRINTF(", "); diff --git a/src/sql/ob_dml_stmt_printer.h b/src/sql/ob_dml_stmt_printer.h index 5f8cac0efa..ead74f3cf9 100644 --- a/src/sql/ob_dml_stmt_printer.h +++ b/src/sql/ob_dml_stmt_printer.h @@ -40,26 +40,18 @@ namespace sql table_item->database_name_ ) : \ table_item->synonym_db_name_; \ ObString table_name = table_item->synonym_name_.empty() ? table_item->table_name_ : table_item->synonym_name_ ; \ - int64_t temp_buf_len = (table_name.length() + database_name.length()) * 4; \ - char temp_buf[temp_buf_len]; \ - ObDataBuffer data_buff(temp_buf, temp_buf_len); \ - if (OB_FAIL(ObCharset::charset_convert(data_buff, table_name, CS_TYPE_UTF8MB4_BIN, \ - print_params_.cs_type_, table_name))) { \ - } else if (OB_FAIL(ObCharset::charset_convert(data_buff, database_name, CS_TYPE_UTF8MB4_BIN, \ - print_params_.cs_type_, database_name))) { \ - } \ - bool is_oracle_mode = lib::is_oracle_mode(); \ - if (table_item->cte_type_ == TableItem::NOT_CTE) { \ + if (table_item->cte_type_ == TableItem::NOT_CTE) { \ if (!database_name.empty()) { \ - DATA_PRINTF(is_oracle_mode ? "\"%.*s\"." : "`%.*s`.", LEN_AND_PTR(database_name)); \ + PRINT_IDENT_WITH_QUOT(database_name); \ + DATA_PRINTF("."); \ } \ - DATA_PRINTF(is_oracle_mode ? "\"%.*s\"" : "`%.*s`", LEN_AND_PTR(table_name)); \ + PRINT_IDENT_WITH_QUOT(table_name); \ if (table_item->synonym_name_.empty() && table_item->is_link_type()) { \ const ObString &dblink_name = table_item->dblink_name_; \ DATA_PRINTF("@%.*s", LEN_AND_PTR(dblink_name)); \ } \ } else { \ - DATA_PRINTF(is_oracle_mode ? "\"%.*s\"" : "`%.*s`", LEN_AND_PTR(table_name)); \ + PRINT_IDENT_WITH_QUOT(table_name); \ } \ } while (0) @@ -67,25 +59,12 @@ namespace sql do { \ ObString database_name = table_item->database_name_; \ ObString table_name = table_item->table_name_; \ - int64_t temp_buf_len = (table_name.length() + database_name.length()) * 4; \ - char temp_buf[temp_buf_len]; \ - ObDataBuffer data_buff(temp_buf, temp_buf_len); \ - if (OB_FAIL(ObCharset::charset_convert(data_buff, table_name, CS_TYPE_UTF8MB4_BIN, \ - print_params_.cs_type_, table_name))) { \ - } else if (OB_FAIL(ObCharset::charset_convert(data_buff, database_name, CS_TYPE_UTF8MB4_BIN, \ - print_params_.cs_type_, database_name))) { \ - } \ - bool is_oracle_mode = lib::is_oracle_mode(); \ - if (table_item->cte_type_ == TableItem::NOT_CTE) { \ + if (table_item->cte_type_ == TableItem::NOT_CTE) { \ if (!database_name.empty()) { \ - PRINT_QUOT; \ - DATA_PRINTF("%.*s", LEN_AND_PTR(database_name)); \ - PRINT_QUOT; \ + PRINT_IDENT_WITH_QUOT(database_name); \ DATA_PRINTF("."); \ } \ - PRINT_QUOT; \ - DATA_PRINTF("%.*s", LEN_AND_PTR(table_name)); \ - PRINT_QUOT; \ + PRINT_IDENT_WITH_QUOT(table_name); \ if (table_item->is_link_type()) { \ const ObString &dblink_name = table_item->dblink_name_; \ if (table_item->is_reverse_link_) { \ @@ -95,19 +74,15 @@ namespace sql } \ } \ } else { \ - PRINT_QUOT; \ - DATA_PRINTF("%.*s", LEN_AND_PTR(table_name)); \ - PRINT_QUOT; \ + PRINT_IDENT_WITH_QUOT(table_name); \ } \ } while (0) #define PRINT_COLUMN_NAME(column_name) \ do {\ if (column_name.empty()) { \ - } else if (lib::is_oracle_mode()) {\ - DATA_PRINTF("\"%.*s\"", LEN_AND_PTR(column_name));\ } else {\ - DATA_PRINTF("`%.*s`", LEN_AND_PTR(column_name));\ + PRINT_IDENT_WITH_QUOT(column_name);\ }\ } while (0); diff --git a/src/sql/ob_insert_all_stmt_printer.cpp b/src/sql/ob_insert_all_stmt_printer.cpp index 6fa4b31975..9ab0e8c988 100644 --- a/src/sql/ob_insert_all_stmt_printer.cpp +++ b/src/sql/ob_insert_all_stmt_printer.cpp @@ -225,9 +225,7 @@ int ObInsertAllStmtPrinter::print_into_table_values(const ObInsertAllStmt *inser ret = OB_ERR_UNEXPECTED; LOG_WARN("column is NULL", K(ret)); } else { - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(column->get_column_name())); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(column->get_column_name()); DATA_PRINTF(","); } } diff --git a/src/sql/ob_insert_stmt_printer.cpp b/src/sql/ob_insert_stmt_printer.cpp index b379d18635..3ac2f9c8d9 100644 --- a/src/sql/ob_insert_stmt_printer.cpp +++ b/src/sql/ob_insert_stmt_printer.cpp @@ -140,9 +140,7 @@ int ObInsertStmtPrinter::print_into() // 临时表的隐藏列,不需要print。TODO 将临时表insert改写由resolver转移到改写阶段后,可以去掉本分支 LOG_DEBUG("do not print column", K(*column)); } else { - PRINT_QUOT; - PRINT_IDENT(column->get_column_name()); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(column->get_column_name()); DATA_PRINTF(","); } } diff --git a/src/sql/ob_select_stmt_printer.cpp b/src/sql/ob_select_stmt_printer.cpp index 65ac6b5b00..5345384a61 100644 --- a/src/sql/ob_select_stmt_printer.cpp +++ b/src/sql/ob_select_stmt_printer.cpp @@ -488,9 +488,7 @@ int ObSelectStmtPrinter::print_select() LOG_WARN("failed to remove double quotation for string", K(ret)); } else { DATA_PRINTF(" AS "); - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(alias_string)); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(alias_string); } } DATA_PRINTF(","); diff --git a/src/sql/ob_sql_utils.cpp b/src/sql/ob_sql_utils.cpp index 34f5490e29..1463eef1a6 100644 --- a/src/sql/ob_sql_utils.cpp +++ b/src/sql/ob_sql_utils.cpp @@ -56,7 +56,9 @@ #include "observer/omt/ob_tenant_srs_mgr.h" #include "sql/executor/ob_maintain_dependency_info_task.h" #include "sql/resolver/ddl/ob_create_view_resolver.h" - +extern "C" { +#include "sql/parser/ob_non_reserved_keywords.h" +} using namespace oceanbase; using namespace oceanbase::sql; using namespace oceanbase::obmysql; @@ -3891,20 +3893,28 @@ int ObSQLUtils::convert_sql_text_to_schema_for_storing(ObIAllocator &allocator, int ObSQLUtils::print_identifier(char *buf, const int64_t buf_len, int64_t &pos, ObCollationType connection_collation, - const common::ObString &identifier_name) + const common::ObString &identifier_name, + bool is_oracle_mode) { int ret = OB_SUCCESS; + ObArenaAllocator allocator("PrintIdentifier"); + ObString print_name; if (OB_ISNULL(buf)) { ret = OB_INVALID_ARGUMENT; LOG_WARN("invalid argument", K(ret)); + } else if (OB_FAIL(generate_new_name_with_escape_character(allocator, + identifier_name, + print_name, + is_oracle_mode))) { + LOG_WARN("failed to generate new name with escape character", K(ret)); } else if (ObCharset::charset_type_by_coll(connection_collation) == CHARSET_UTF8MB4) { - if (OB_UNLIKELY(pos + identifier_name.length() > buf_len)) { + if (OB_UNLIKELY(pos + print_name.length() > buf_len)) { ret = OB_SIZE_OVERFLOW; - LOG_WARN("size overflow", K(ret), K(identifier_name)); + LOG_WARN("size overflow", K(ret), K(print_name)); } else { - MEMCPY(buf + pos, identifier_name.ptr(), identifier_name.length()); - pos += identifier_name.length(); + MEMCPY(buf + pos, print_name.ptr(), print_name.length()); + pos += print_name.length(); } } else if (OB_UNLIKELY(buf_len <= pos)) { ret = OB_SIZE_OVERFLOW; @@ -3912,8 +3922,8 @@ int ObSQLUtils::print_identifier(char *buf, const int64_t buf_len, int64_t &pos, } else { uint32_t result_len = 0; if (OB_FAIL(ObCharset::charset_convert(CS_TYPE_UTF8MB4_BIN, - identifier_name.ptr(), - identifier_name.length(), + print_name.ptr(), + print_name.length(), connection_collation, buf + pos, buf_len - pos, @@ -5043,3 +5053,39 @@ int ObSQLUtils::find_synonym_ref_obj(const uint64_t database_id, } return ret; } + +int ObSQLUtils::print_identifier_require_quotes(ObCollationType collation_type, + const ObString &ident, + bool &require) +{ + int ret = OB_SUCCESS; + bool pure_digit = true; + const ObCharsetInfo *info = ObCharset::get_charset(collation_type); + require = false; + if (OB_ISNULL(info)) { + ret = OB_INVALID_ARGUMENT; + OB_LOG(WARN, "arguemnt is invalid", K(ret)); + } else if (ident.length() > 0 && ident[0] == '$') { + require = true; + } + for (int64_t i = 0; OB_SUCC(ret) && !require && i < ident.length(); i++) { + char ch = ident[i]; + uint length = ob_mbcharlen(info, ch); + if (length == 0) { + require = true; + } else if (length == 1 && !oceanbase::sql::MYSQL_IDENTIFIER_FALGS[static_cast(ch)]) { + require = true; + } else if (length == 1 && (ch < '0' || ch > '9')) { + pure_digit = false; + } + } + if (OB_FAIL(ret)) { + } else if (pure_digit) { + require = true; + } else if (require) { + //do nothing + } else if (-1 != mysql_sql_reserved_keyword_lookup(ident.ptr())) { + require = true; + } + return ret; +} \ No newline at end of file diff --git a/src/sql/ob_sql_utils.h b/src/sql/ob_sql_utils.h index d208777969..e872ecb189 100644 --- a/src/sql/ob_sql_utils.h +++ b/src/sql/ob_sql_utils.h @@ -552,7 +552,8 @@ public: static int print_identifier(char *buf, const int64_t buf_len, int64_t &pos, common::ObCollationType connection_collation, - const common::ObString &identifier_name); + const common::ObString &identifier_name, + bool is_oracle_mode); static bool is_one_part_table_can_skip_part_calc(const share::schema::ObTableSchema &schema); static int create_encode_sortkey_expr(ObRawExprFactory &expr_factory, @@ -622,6 +623,10 @@ public: share::schema::ObObjectType &obj_type, uint64_t &schema_version); static bool check_need_disconnect_parser_err(const int ret_code); + + static int print_identifier_require_quotes(ObCollationType collation_type, + const ObString &ident, + bool &require); private: static int check_ident_name(const common::ObCollationType cs_type, common::ObString &name, const bool check_for_path_char, const int64_t max_ident_len); diff --git a/src/sql/resolver/expr/ob_raw_expr_printer.cpp b/src/sql/resolver/expr/ob_raw_expr_printer.cpp index 0bad7d117b..d9e8e285eb 100644 --- a/src/sql/resolver/expr/ob_raw_expr_printer.cpp +++ b/src/sql/resolver/expr/ob_raw_expr_printer.cpp @@ -105,9 +105,7 @@ int ObRawExprPrinter::print(ObRawExpr *expr) && scope_ != T_ORDER_SCOPE) { //expr is a alias column ref //alias column target list - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(expr->get_alias_column_name())); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(expr->get_alias_column_name()); } else { switch (expr->get_expr_class()) { case ObRawExpr::EXPR_CONST: { @@ -378,101 +376,61 @@ int ObRawExprPrinter::print(ObColumnRefRawExpr *expr) PRINT_EXPR(expr->get_dependant_expr()); } else { ObArenaAllocator arena_alloc; - ObString col_name; - if (OB_FAIL(ObSQLUtils::generate_new_name_with_escape_character(allocator, expr->get_column_name(), - col_name, is_oracle_mode))) { - LOG_WARN("fail to generate new name with escape character", K(ret)); - } else if (is_oracle_mode && + ObString col_name = expr->get_column_name(); + if (is_oracle_mode && OB_FAIL(ObSelectStmtPrinter::remove_double_quotation_for_string(col_name, arena_alloc))) { LOG_WARN("failed to remove double quotation for string", K(ret)); - } else if (OB_FAIL(ObCharset::charset_convert(allocator, - col_name, - CS_TYPE_UTF8MB4_BIN, - print_params_.cs_type_, - col_name))) { - LOG_WARN("fail to convert charset", K(ret)); } else if (print_params_.for_dblink_) { if (!expr->is_cte_generated_column() && !expr->get_database_name().empty()) { - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(expr->get_database_name())); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(expr->get_database_name()); DATA_PRINTF("."); } if (!expr->get_table_name().empty()) { ObString table_name = expr->get_table_name(); - CONVERT_CHARSET_FOR_RPINT(allocator, table_name); - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(table_name)); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(table_name); DATA_PRINTF("."); } - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(col_name)); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(col_name); } else if (expr->is_cte_generated_column()) { ObString table_name = expr->get_synonym_name().empty() ? expr->get_table_name() : expr->get_synonym_name(); if (OB_SUCC(ret)) { // note: expr's table_name is equal to alias if table's alias is not empty, - CONVERT_CHARSET_FOR_RPINT(allocator, table_name); - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(table_name)); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(table_name); DATA_PRINTF("."); - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(col_name)); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(col_name); } } else if (OB_UNLIKELY(only_column_namespace_)) { - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(col_name)); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(col_name); } else if (expr->is_from_alias_table()) { - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(expr->get_table_name())); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(expr->get_table_name()); DATA_PRINTF("."); - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(col_name)); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(col_name); } else { if (!expr->get_synonym_name().empty() && !expr->get_synonym_db_name().empty()) { ObString synonyn_db_name = expr->get_synonym_db_name(); - CONVERT_CHARSET_FOR_RPINT(allocator, synonyn_db_name); - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(synonyn_db_name)); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(synonyn_db_name); DATA_PRINTF("."); } else if (!expr->get_synonym_name().empty() && expr->get_synonym_db_name().empty()) { // do nothing, synonym database name is not explicit } else if (expr->get_database_name().length() > 0) { ObString database_name = expr->get_database_name(); - CONVERT_CHARSET_FOR_RPINT(allocator, database_name); - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(database_name)); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(database_name); DATA_PRINTF("."); } ObString table_name = expr->get_synonym_name().empty() ? expr->get_table_name() : expr->get_synonym_name(); if (OB_SUCC(ret)) { - CONVERT_CHARSET_FOR_RPINT(allocator, table_name); // note: expr's table_name is equal to alias if table's alias is not empty, if (!table_name.empty()) { - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(table_name)); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(table_name); DATA_PRINTF("."); - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(col_name)); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(col_name); } else { // oracle allow derived table without alias name, table_name is empty here. // e.g.: select * from (select 1 from dual) - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(col_name)); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(col_name); } } } @@ -2844,19 +2802,13 @@ int ObRawExprPrinter::print(ObSysFunRawExpr *expr) LOG_WARN("param count should be equal 1", K(ret), K(seq_expr->get_param_count())); } else { if (!seq_expr->get_database_name().empty()) { - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(seq_expr->get_database_name())); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(seq_expr->get_database_name()); DATA_PRINTF("."); } if (!seq_expr->get_name().empty() && !seq_expr->get_action().empty()) { - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(seq_expr->get_name())); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(seq_expr->get_name()); DATA_PRINTF("."); - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(seq_expr->get_action())); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(seq_expr->get_action()); } else { ret = OB_ERR_UNEXPECTED; LOG_WARN("sequence should sepcify format as seqname.action", K(ret)); @@ -3032,13 +2984,9 @@ int ObRawExprPrinter::print(ObSysFunRawExpr *expr) sub_pk_expr = static_cast(expr->get_param_expr(1)); ObString table_name = sub_pk_expr->get_table_name(); if (!table_name.empty()) { - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(table_name)); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(table_name); DATA_PRINTF("."); - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(ObString(OB_HIDDEN_LOGICAL_ROWID_COLUMN_NAME))); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(ObString(OB_HIDDEN_LOGICAL_ROWID_COLUMN_NAME)); } } break; @@ -3171,9 +3119,7 @@ int ObRawExprPrinter::print(ObSysFunRawExpr *expr) break; } case T_FUN_UDF: { - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(func_name)); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(func_name); OZ(inner_print_fun_params(*expr)); break; } @@ -3237,21 +3183,15 @@ int ObRawExprPrinter::print(ObUDFRawExpr *expr) if (!print_params_.for_dblink_ && !expr->get_database_name().empty() && expr->get_database_name().case_compare("oceanbase") != 0) { - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(expr->get_database_name())); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(expr->get_database_name()); DATA_PRINTF("."); } if (!expr->get_package_name().empty() && !expr->get_is_udt_cons()) { - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(expr->get_package_name())); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(expr->get_package_name()); DATA_PRINTF("."); } - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(expr->get_func_name())); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(expr->get_func_name()); DATA_PRINTF("("); ObIArray ¶ms_type = expr->get_params_type(); @@ -3266,9 +3206,7 @@ int ObRawExprPrinter::print(ObUDFRawExpr *expr) // do not print construnct null self argument } else { if (!params_name.at(i).empty()) { - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(params_name.at(i))); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(params_name.at(i)); DATA_PRINTF("=>"); } PRINT_EXPR(expr->get_param_expr(i)); @@ -3397,15 +3335,11 @@ int ObRawExprPrinter::print(ObWinFunRawExpr *expr) } if (OB_SUCC(ret)) { if(!database.empty()){ - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(database)); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(database); DATA_PRINTF("."); } if (T_FUN_PL_AGG_UDF == type) { - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(symbol)); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(symbol); } else { DATA_PRINTF("%.*s", LEN_AND_PTR(symbol)); } @@ -4294,9 +4228,7 @@ int ObRawExprPrinter::print_cast_type(ObRawExpr *expr) } else if (OB_FAIL(schema_guard_->get_udt_info(dest_tenant_id, udt_id, dest_info))) { LOG_WARN("failed to get udt info", K(ret)); } else { - PRINT_QUOT; - DATA_PRINTF("%.*s", LEN_AND_PTR(dest_info->get_type_name())); - PRINT_QUOT; + PRINT_IDENT_WITH_QUOT(dest_info->get_type_name()); } break; } diff --git a/src/sql/resolver/expr/ob_raw_expr_printer.h b/src/sql/resolver/expr/ob_raw_expr_printer.h index 3d306ec55e..fee4e25d07 100644 --- a/src/sql/resolver/expr/ob_raw_expr_printer.h +++ b/src/sql/resolver/expr/ob_raw_expr_printer.h @@ -45,8 +45,10 @@ class ObRawExprPrinter #define PRINT_IDENT(ident_str) \ do { \ - if (OB_SUCC(ret) && OB_FAIL(ObSQLUtils::print_identifier( \ - buf_, buf_len_, (*pos_), print_params_.cs_type_, ident_str))) { \ + if (OB_SUCC(ret) && OB_FAIL(ObSQLUtils::print_identifier(buf_, buf_len_, (*pos_), \ + print_params_.cs_type_, \ + ident_str, \ + lib::is_oracle_mode()))) { \ LOG_WARN("fail to print ident str", K(ret), K(ident_str)); \ } \ } while(0) @@ -89,6 +91,13 @@ class ObRawExprPrinter DATA_PRINTF(" "); \ PRINT_QUOT; +#define PRINT_IDENT_WITH_QUOT(ident_str) \ + do { \ + PRINT_QUOT; \ + PRINT_IDENT(ident_str); \ + PRINT_QUOT; \ + } while (0) + // cast函数在parse阶段用到这两个宏, 但定义在sql_parse_tab.c中 // cast函数功能不完善,beta之前不会修改, 先定义在这里 // TODO@nijia.nj diff --git a/src/sql/session/ob_basic_session_info.cpp b/src/sql/session/ob_basic_session_info.cpp index 44316bccb5..d819e2461e 100644 --- a/src/sql/session/ob_basic_session_info.cpp +++ b/src/sql/session/ob_basic_session_info.cpp @@ -3544,6 +3544,10 @@ int ObBasicSessionInfo::get_show_ddl_in_compat_mode(bool &show_ddl_in_compat_mod return get_bool_sys_var(SYS_VAR__SHOW_DDL_IN_COMPAT_MODE, show_ddl_in_compat_mode); } +int ObBasicSessionInfo::get_sql_quote_show_create(bool &sql_quote_show_create) const +{ + return get_bool_sys_var(SYS_VAR_SQL_QUOTE_SHOW_CREATE, sql_quote_show_create); +} //////////////////////////////////////////////////////////////// int ObBasicSessionInfo::replace_user_variables(const ObSessionValMap &user_var_map) { diff --git a/src/sql/session/ob_basic_session_info.h b/src/sql/session/ob_basic_session_info.h index 3a2cda6dc8..217063a9da 100644 --- a/src/sql/session/ob_basic_session_info.h +++ b/src/sql/session/ob_basic_session_info.h @@ -499,6 +499,7 @@ public: return common::OB_SUCCESS; } int get_show_ddl_in_compat_mode(bool &show_ddl_in_compat_mode) const; + int get_sql_quote_show_create(bool &sql_quote_show_create) const; common::ObConsistencyLevel get_consistency_level() const { return consistency_level_; }; bool is_zombie() const { return SESSION_KILLED == get_session_state();} bool is_query_killed() const;