diff --git a/contrib/shark/dbcc.cpp b/contrib/shark/dbcc.cpp index d4e064b9f0a486dfb6934cb46ac716eafd209fb7..97b2bc893e1cd01f78334c86dc3af1ca8dfa48f7 100644 --- a/contrib/shark/dbcc.cpp +++ b/contrib/shark/dbcc.cpp @@ -22,13 +22,22 @@ Datum dbcc_check_ident_no_reseed(PG_FUNCTION_ARGS) char result[DBCC_RESULT_MAX_LENGTH] = {0}; errno_t rc = EOK; bool withmsg = true; + bool reseed_to_max = false; if (!fcinfo->argnull[1]) { withmsg = !PG_GETARG_BOOL(1); } + if (!fcinfo->argnull[2]) { + reseed_to_max = PG_GETARG_BOOL(2); + } + get_last_value_and_max_value(txt, &last_value, ¤t_max_value); + if (reseed_to_max && last_value < current_max_value) { + get_and_reset_last_value(txt, current_max_value, true); + } + if (!withmsg) { PG_RETURN_NULL(); } diff --git a/contrib/shark/expected/test_dbcc.out b/contrib/shark/expected/test_dbcc.out index 6b41a6ea241c5a1fc7368be953d3455bc1e83b55..9389aac518109870ed34a96c107c16b416259556 100644 --- a/contrib/shark/expected/test_dbcc.out +++ b/contrib/shark/expected/test_dbcc.out @@ -131,6 +131,106 @@ PL/pgSQL function test_procedure_test1(integer) line 2 at PERFORM drop table Employees; drop table Employees_ne; drop procedure test_procedure_test1(int); +CREATE TABLE Employees ( + EmployeeID serial , + Name VARCHAR(100) NOT NULL + ); +NOTICE: CREATE TABLE will create implicit sequence "employees_employeeid_seq" for serial column "employees.employeeid" +insert into Employees(Name) values ('zhangsan'); +insert into Employees(Name) values ('lisi'); +insert into Employees(Name) values ('wangwu'); +insert into Employees(Name) values ('heliu'); +DBCC CHECKIDENT ('Employees', NORESEED); +NOTICE: "Checking identity information: current identity value '4', current column value '4'." +CONTEXT: referenced column: dbcc_check_ident_no_reseed + dbcc_check_ident_no_reseed +-------------------------------------------------------------------------------------- + Checking identity information: current identity value '4', current column value '4'. +(1 row) + +DBCC CHECKIDENT ('Employees', RESEED, 1); +NOTICE: "Checking identity information: current identity value '4'." +CONTEXT: referenced column: dbcc_check_ident_reseed + dbcc_check_ident_reseed +------------------------------------------------------------ + Checking identity information: current identity value '4'. +(1 row) + +DBCC CHECKIDENT ('Employees', NORESEED); +NOTICE: "Checking identity information: current identity value '1', current column value '4'." +CONTEXT: referenced column: dbcc_check_ident_no_reseed + dbcc_check_ident_no_reseed +-------------------------------------------------------------------------------------- + Checking identity information: current identity value '1', current column value '4'. +(1 row) + +DBCC CHECKIDENT ('Employees'); +NOTICE: "Checking identity information: current identity value '1', current column value '4'." +CONTEXT: referenced column: dbcc_check_ident_no_reseed + dbcc_check_ident_no_reseed +-------------------------------------------------------------------------------------- + Checking identity information: current identity value '1', current column value '4'. +(1 row) + +DBCC CHECKIDENT ('Employees', NORESEED); +NOTICE: "Checking identity information: current identity value '4', current column value '4'." +CONTEXT: referenced column: dbcc_check_ident_no_reseed + dbcc_check_ident_no_reseed +-------------------------------------------------------------------------------------- + Checking identity information: current identity value '4', current column value '4'. +(1 row) + +drop table Employees; +CREATE TABLE Employees ( + EmployeeID serial , + Name VARCHAR(100) NOT NULL + ); +NOTICE: CREATE TABLE will create implicit sequence "employees_employeeid_seq" for serial column "employees.employeeid" +insert into Employees(Name) values ('zhangsan'); +insert into Employees(Name) values ('lisi'); +insert into Employees(Name) values ('wangwu'); +insert into Employees(Name) values ('heliu'); +DBCC CHECKIDENT ('Employees', NORESEED); +NOTICE: "Checking identity information: current identity value '4', current column value '4'." +CONTEXT: referenced column: dbcc_check_ident_no_reseed + dbcc_check_ident_no_reseed +-------------------------------------------------------------------------------------- + Checking identity information: current identity value '4', current column value '4'. +(1 row) + +DBCC CHECKIDENT ('Employees', RESEED, 1); +NOTICE: "Checking identity information: current identity value '4'." +CONTEXT: referenced column: dbcc_check_ident_reseed + dbcc_check_ident_reseed +------------------------------------------------------------ + Checking identity information: current identity value '4'. +(1 row) + +DBCC CHECKIDENT ('Employees', NORESEED); +NOTICE: "Checking identity information: current identity value '1', current column value '4'." +CONTEXT: referenced column: dbcc_check_ident_no_reseed + dbcc_check_ident_no_reseed +-------------------------------------------------------------------------------------- + Checking identity information: current identity value '1', current column value '4'. +(1 row) + +DBCC CHECKIDENT ('Employees', RESEED); +NOTICE: "Checking identity information: current identity value '1', current column value '4'." +CONTEXT: referenced column: dbcc_check_ident_no_reseed + dbcc_check_ident_no_reseed +-------------------------------------------------------------------------------------- + Checking identity information: current identity value '1', current column value '4'. +(1 row) + +DBCC CHECKIDENT ('Employees', NORESEED); +NOTICE: "Checking identity information: current identity value '4', current column value '4'." +CONTEXT: referenced column: dbcc_check_ident_no_reseed + dbcc_check_ident_no_reseed +-------------------------------------------------------------------------------------- + Checking identity information: current identity value '4', current column value '4'. +(1 row) + +drop table Employees; -- part1: dbcc check RESEED CREATE TABLE Employees ( EmployeeID serial , @@ -229,11 +329,11 @@ select * from Employees order by 1, 2; ALTER SEQUENCE employees_employeeid_seq MINVALUE -100; DBCC CHECKIDENT ('Employees', RESEED); -NOTICE: "Checking identity information: current identity value '3'." -CONTEXT: referenced column: dbcc_check_ident_reseed - dbcc_check_ident_reseed ------------------------------------------------------------- - Checking identity information: current identity value '3'. +NOTICE: "Checking identity information: current identity value '3', current column value '3'." +CONTEXT: referenced column: dbcc_check_ident_no_reseed + dbcc_check_ident_no_reseed +-------------------------------------------------------------------------------------- + Checking identity information: current identity value '3', current column value '3'. (1 row) DBCC CHECKIDENT ('Employees', RESEED, -2); diff --git a/contrib/shark/shark--1.0.sql b/contrib/shark/shark--1.0.sql index 7e1713921ad8d0dc240847943eb5245f6666d2d8..d1651bd5921a8ba8c6eb75f160a434624da8ca9f 100644 --- a/contrib/shark/shark--1.0.sql +++ b/contrib/shark/shark--1.0.sql @@ -50,7 +50,7 @@ create trusted language pltsql validator pltsql_validator; -CREATE FUNCTION dbcc_check_ident_no_reseed(varchar, boolean) RETURNS varchar as 'MODULE_PATHNAME', 'dbcc_check_ident_no_reseed' LANGUAGE C STRICT STABLE; +CREATE FUNCTION dbcc_check_ident_no_reseed(varchar, boolean, boolean) RETURNS varchar as 'MODULE_PATHNAME', 'dbcc_check_ident_no_reseed' LANGUAGE C STRICT STABLE; CREATE FUNCTION dbcc_check_ident_reseed(varchar, bigint, boolean) RETURNS varchar as 'MODULE_PATHNAME', 'dbcc_check_ident_reseed' LANGUAGE C STABLE; grant usage on language pltsql to public; diff --git a/contrib/shark/sql/test_dbcc.sql b/contrib/shark/sql/test_dbcc.sql index fc5b8337509a83ea0631b566b20195390e26c6d8..131634e0ef511f7671a72e0e2d2c1ce4ba33e095 100644 --- a/contrib/shark/sql/test_dbcc.sql +++ b/contrib/shark/sql/test_dbcc.sql @@ -78,6 +78,51 @@ drop table Employees_ne; drop procedure test_procedure_test1(int); +CREATE TABLE Employees ( + EmployeeID serial , + Name VARCHAR(100) NOT NULL + ); + +insert into Employees(Name) values ('zhangsan'); +insert into Employees(Name) values ('lisi'); +insert into Employees(Name) values ('wangwu'); +insert into Employees(Name) values ('heliu'); + +DBCC CHECKIDENT ('Employees', NORESEED); + +DBCC CHECKIDENT ('Employees', RESEED, 1); + +DBCC CHECKIDENT ('Employees', NORESEED); + +DBCC CHECKIDENT ('Employees'); + +DBCC CHECKIDENT ('Employees', NORESEED); + +drop table Employees; + + +CREATE TABLE Employees ( + EmployeeID serial , + Name VARCHAR(100) NOT NULL + ); + +insert into Employees(Name) values ('zhangsan'); +insert into Employees(Name) values ('lisi'); +insert into Employees(Name) values ('wangwu'); +insert into Employees(Name) values ('heliu'); + +DBCC CHECKIDENT ('Employees', NORESEED); + +DBCC CHECKIDENT ('Employees', RESEED, 1); + +DBCC CHECKIDENT ('Employees', NORESEED); + +DBCC CHECKIDENT ('Employees', RESEED); + +DBCC CHECKIDENT ('Employees', NORESEED); + +drop table Employees; + -- part1: dbcc check RESEED CREATE TABLE Employees ( EmployeeID serial , diff --git a/contrib/shark/src/backend_parser/gram-tsql-epilogue.y.cpp b/contrib/shark/src/backend_parser/gram-tsql-epilogue.y.cpp index bf015b86921aeb47b413393019fe73a4f4ac5d28..122b724fdf716f1b55b1a775926333ea67198bd2 100644 --- a/contrib/shark/src/backend_parser/gram-tsql-epilogue.y.cpp +++ b/contrib/shark/src/backend_parser/gram-tsql-epilogue.y.cpp @@ -41,10 +41,10 @@ static List* make_func_call_func(List* funcname, List* args) return (list_make1(restarget)); } -static List* make_no_reseed_func(char* table_name, bool with_no_msgs) +static List* make_no_reseed_func(char* table_name, bool with_no_msgs, bool reseed_to_max) { List* funcname = list_make1(makeString("dbcc_check_ident_no_reseed")); - List* args = list_make2(makeStringConst(table_name, -1), makeBoolConst(with_no_msgs, false)); + List* args = list_make3(makeStringConst(table_name, -1), makeBoolConst(with_no_msgs, false), makeBoolConst(reseed_to_max, false)); return make_func_call_func(funcname, args); } diff --git a/contrib/shark/src/backend_parser/gram-tsql-prologue.y.h b/contrib/shark/src/backend_parser/gram-tsql-prologue.y.h index 621c88131e9f1e2a96adc6ff68f9c508cd311092..53123d6cecaee25943f94f22ed38ba20800addc2 100644 --- a/contrib/shark/src/backend_parser/gram-tsql-prologue.y.h +++ b/contrib/shark/src/backend_parser/gram-tsql-prologue.y.h @@ -1,6 +1,6 @@ static void pgtsql_base_yyerror(YYLTYPE * yylloc, core_yyscan_t yyscanner, const char *msg); List *TsqlSystemFuncName2(char *name); -static List* make_no_reseed_func(char* table_name, bool with_no_msgs); +static List* make_no_reseed_func(char* table_name, bool with_no_msgs, bool reseed_to_max); static List* make_reseed_func(char* table_name, Node* new_seed, bool with_no_msgs); static List* make_func_call_func(List* funcname, List* args); static char* quote_identifier_wrapper(char* ident, core_yyscan_t yyscanner); \ No newline at end of file diff --git a/contrib/shark/src/backend_parser/gram-tsql-rule.y b/contrib/shark/src/backend_parser/gram-tsql-rule.y index 20ae2c4e5acc24a9ce3a54300af98ab4c6a42338..5e032592c92f27db952e54b6c94a47815c176d5f 100644 --- a/contrib/shark/src/backend_parser/gram-tsql-rule.y +++ b/contrib/shark/src/backend_parser/gram-tsql-rule.y @@ -517,7 +517,7 @@ DBCCCheckIdentStmt: { SelectStmt *n = makeNode(SelectStmt); n->distinctClause = NIL; - n->targetList = make_no_reseed_func(quote_identifier_wrapper($4, yyscanner), $8); + n->targetList = make_no_reseed_func(quote_identifier_wrapper($4, yyscanner), $8, false); n->intoClause = NULL; n->fromClause = NIL; n->whereClause = NULL; @@ -530,7 +530,7 @@ DBCCCheckIdentStmt: { SelectStmt *n = makeNode(SelectStmt); n->distinctClause = NIL; - n->targetList = make_no_reseed_func(quote_identifier_wrapper($4, yyscanner), $6); + n->targetList = make_no_reseed_func(quote_identifier_wrapper($4, yyscanner), $6, true); n->intoClause = NULL; n->fromClause = NIL; n->whereClause = NULL; @@ -556,7 +556,7 @@ DBCCCheckIdentStmt: { SelectStmt *n = makeNode(SelectStmt); n->distinctClause = NIL; - n->targetList = make_reseed_func(quote_identifier_wrapper($4, yyscanner), makeNullAConst(@6), $8); + n->targetList = make_no_reseed_func(quote_identifier_wrapper($4, yyscanner), $8, true); n->intoClause = NULL; n->fromClause = NIL; n->whereClause = NULL;