From f7fbc86700e433cfb27ef580cb78fbc08f58e5e8 Mon Sep 17 00:00:00 2001 From: wangpingyun <2418191738@qq.com> Date: Fri, 25 Apr 2025 10:01:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dobject=5Fid=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E4=B8=8D=E6=94=AF=E6=8C=81dbnmae..objname=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contrib/shark/expected/objectproperty.out | 32 ++++++++-- contrib/shark/sql/objectproperty.sql | 3 + contrib/shark/varlena.cpp | 78 +++++++++++++++-------- 3 files changed, 83 insertions(+), 30 deletions(-) diff --git a/contrib/shark/expected/objectproperty.out b/contrib/shark/expected/objectproperty.out index 21719f109e..5cf1f111c8 100644 --- a/contrib/shark/expected/objectproperty.out +++ b/contrib/shark/expected/objectproperty.out @@ -74,6 +74,24 @@ select object_id('sys.trigger_update_total_grade'); (1 row) set search_path = 'sys'; +select object_id('contrib_regression.students'); + object_id +----------- + +(1 row) + +select object_id('contrib_regression..students'); + object_id +----------- +--?.* +(1 row) + +select object_id('contrib_regression...students'); + object_id +----------- + +(1 row) + select object_id('students', 'U'); object_id ----------- @@ -117,8 +135,11 @@ select object_id('sys.students'); (1 row) select object_id('contrib_regression.sys.students'); -ERROR: Can only do lookup in current database. -CONTEXT: referenced column: object_id + object_id +----------- +--?.* +(1 row) + select objectproperty(object_id('students'), 'istable') as istable; istable --------- @@ -937,8 +958,11 @@ select object_id('othersys.students'); (1 row) select object_id('otherdb.sys.students'); -ERROR: Can only do lookup in current database. -CONTEXT: referenced column: object_id + object_id +----------- + +(1 row) + select objectproperty('', 'istable') as istable; istable --------- diff --git a/contrib/shark/sql/objectproperty.sql b/contrib/shark/sql/objectproperty.sql index 59b3970379..8c6bc109b5 100644 --- a/contrib/shark/sql/objectproperty.sql +++ b/contrib/shark/sql/objectproperty.sql @@ -49,6 +49,9 @@ select object_id('sys.insert_student'); select object_id('sys.trigger_update_total_grade'); set search_path = 'sys'; +select object_id('contrib_regression.students'); +select object_id('contrib_regression..students'); +select object_id('contrib_regression...students'); select object_id('students', 'U'); select object_id('students_pkey', 'PK'); diff --git a/contrib/shark/varlena.cpp b/contrib/shark/varlena.cpp index 66064376b4..61952ef498 100644 --- a/contrib/shark/varlena.cpp +++ b/contrib/shark/varlena.cpp @@ -567,42 +567,68 @@ object_id_internal(PG_FUNCTION_ARGS) if (strlen(object_name) < 1) { PG_RETURN_NULL(); } - - List* nameList = stringToQualifiedNameList(object_name); - + char* db_name = NULL; char* schema_name = NULL; char* obj_name = NULL; - switch (list_length(nameList)) - { - case 1: - obj_name = strVal(linitial(nameList)); - break; - case 2: - obj_name = strVal(lsecond(nameList)); - schema_name = get_current_physical_schema_name(strVal(linitial(nameList))); - break; - case 3: - obj_name = strVal(lthird(nameList)); - schema_name = get_current_physical_schema_name(strVal(lsecond(nameList))); - db_name = strVal(linitial(nameList)); - break; - default: - ereport(ERROR, - (errcode(ERRCODE_SYNTAX_ERROR), - errmsg("improper qualified name (too many dotted names): %s", NameListToString(nameList)))); - break; + + char *tok = object_name; + while (*tok) { + // Detected two consecutive points + if (*tok == '.' && *(tok + 1) == '.') { + *tok = '\0'; + db_name = object_name; + tok += 2; + break; + } else { + tok++; + } + } + if(db_name != NULL) { + obj_name = tok; + } else { + List* nameList; + PG_TRY(); + { + nameList = stringToQualifiedNameList(object_name); + } + PG_CATCH(); + { + FlushErrorState(); + PG_RETURN_NULL(); + } + PG_END_TRY(); + switch (list_length(nameList)) + { + case 1: + obj_name = strVal(linitial(nameList)); + break; + case 2: + obj_name = strVal(lsecond(nameList)); + schema_name = get_current_physical_schema_name(strVal(linitial(nameList))); + break; + case 3: + obj_name = strVal(lthird(nameList)); + schema_name = get_current_physical_schema_name(strVal(lsecond(nameList))); + db_name = strVal(linitial(nameList)); + break; + default: + PG_RETURN_NULL(); + break; + } } if (obj_name == NULL || strlen(obj_name) < 1) { PG_RETURN_NULL(); } - if (db_name != NULL && db_name != get_and_check_db_name(u_sess->proc_cxt.MyDatabaseId, true)) { - ereport(ERROR, - (errcode(ERRCODE_UNDEFINED_DATABASE), - errmsg("Can only do lookup in current database."))); + char* current_db = get_and_check_db_name(u_sess->proc_cxt.MyDatabaseId, false); + if (db_name != NULL && strcmp(db_name, current_db) != 0) { + pfree_ext(current_db); + PG_RETURN_NULL(); } + pfree_ext(current_db); + Oid id = search_oid_in_schema(schema_name, obj_name, object_type); if (id == InvalidOid) { PG_RETURN_NULL(); -- Gitee