SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `db_version`;
CREATE TABLE `db_version`  (
                               `id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
                               `version` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '版本号',
                               `created` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
                               `remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '备注',
                               PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '数据版本' ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;


/* 创建函数Pro_Temp_ColumnWork操作表字段 */
DROP PROCEDURE IF EXISTS Pro_Temp_ColumnWork;
CREATE  PROCEDURE `Pro_Temp_ColumnWork` ( TableName VARCHAR ( 50 ), ColumnName VARCHAR ( 50 ), SqlStr VARCHAR ( 4000 ), CType INT ) BEGIN
    DECLARE
Rows1 INT;

    SET Rows1 = 0;
SELECT
    COUNT(*) INTO Rows1
FROM
    INFORMATION_SCHEMA.COLUMNS
WHERE
        table_schema = DATABASE ()
  AND upper( table_name )= TableName
  AND upper( column_name )= ColumnName;
IF
( CType = 1 AND Rows1 <= 0 ) THEN

        SET SqlStr := CONCAT( 'ALTER TABLE ', TableName, ' ADD COLUMN ', ColumnName, ' ', SqlStr );

    ELSEIF ( CType = 2 AND Rows1 > 0 ) THEN

        SET SqlStr := CONCAT( 'ALTER TABLE ', TableName, ' MODIFY  ', ColumnName, ' ', SqlStr );

    ELSEIF ( CType = 3 AND Rows1 > 0 ) THEN

        SET SqlStr := CONCAT( 'ALTER TABLE  ', TableName, ' DROP COLUMN  ', ColumnName );
ELSE
        SET SqlStr := '';

END IF;
    IF
( SqlStr <> '' ) THEN

        SET @SQL1 = SqlStr;
PREPARE stmt1
    FROM
    @SQL1;
EXECUTE stmt1;

END IF;

END;
/** 函数创建结束 **/

/*创建定义普通索引函数*/
DROP PROCEDURE IF EXISTS Modify_index;

CREATE PROCEDURE Modify_index (
    TableName VARCHAR ( 50 ),
    ColumnNames VARCHAR ( 500 ),
    idx_name VARCHAR ( 50 ),
    idx_type VARCHAR ( 50 )) BEGIN
    DECLARE
Rows1 int;
    DECLARE
SqlStr VARCHAR(4000);
    DECLARE
target_database VARCHAR ( 100 );
SELECT DATABASE
           () INTO target_database;

SET Rows1 = 0;
SELECT
    COUNT(*) INTO Rows1
FROM
    information_schema.statistics
WHERE
        table_schema = DATABASE ()
  AND upper( table_name )= upper(TableName)
  AND upper( index_name )= upper(idx_name);
IF Rows1<=0 THEN
        SET SqlStr := CONCAT( 'alter table ', TableName, ' ADD INDEX ', idx_name, '(', ColumnNames, ') USING ', idx_type );
END IF;
    IF
( SqlStr <> '' ) THEN

        SET @SQL1 = SqlStr;
PREPARE stmt1
    FROM
    @SQL1;
EXECUTE stmt1;

END IF;

END;
/*创建定义普通索引函数结束*/
