본문으로 바로가기

MYSQL TO VO

category DB/MYSQL 2021. 5. 4. 14:08
SELECT   CONCAT(
                    '/* ',
                    CASE WHEN LENGTH(COLUMN_COMMENT) > 0 THEN 
                        COLUMN_COMMENT 
                    ELSE 
                        COLUMN_NAME 
                    END,
                    ' */',
                    CHAR(13),
                    'private',
                    CASE 
                        WHEN LOWER(DATA_TYPE) = 'varchar' THEN ' String '
                        WHEN INSTR(LOWER(DATA_TYPE), 'int') > 0 THEN  ' Integer ' 
                        WHEN LOWER(DATA_TYPE) = 'text' THEN ' String ' 
                        WHEN LOWER(DATA_TYPE) = 'datetime' THEN ' String '
                        ELSE ' Object '
                    END, 
                    CONCAT(
                        TRIM(CN1), 
                        TRIM(CONCAT(UPPER(SUBSTRING(CN2, 1, 1)) , SUBSTRING(CN2, 2, LENGTH(CN2)))), 
                        TRIM(CONCAT(UPPER(SUBSTRING(CN3, 1, 1)) , SUBSTRING(CN3, 2, LENGTH(CN3)))),
                        TRIM(CONCAT(UPPER(SUBSTRING(CN4, 1, 1)) , SUBSTRING(CN4, 2, LENGTH(CN4)))),
                        TRIM(CONCAT(UPPER(SUBSTRING(CN5, 1, 1)) , SUBSTRING(CN5, 2, LENGTH(CN5))))
                    ), 
                    ';',
                    CHAR(13),
                    CHAR(13)
                )
FROM
(
SELECT   B.COLUMN_NAME,  
                CASE WHEN LENGTH(B.COLUMN_NAME) - LENGTH(REPLACE(B.COLUMN_NAME, '_', '')) + 1 >= 1 THEN 
                    SUBSTRING_INDEX(SUBSTRING_INDEX(LCASE(B.COLUMN_NAME), '_', 1), '_', -1) 
                ELSE ' ' END AS CN1,
                CASE WHEN LENGTH(B.COLUMN_NAME) - LENGTH(REPLACE(B.COLUMN_NAME, '_', '')) + 1 >= 2 THEN 
                    SUBSTRING_INDEX(SUBSTRING_INDEX(LCASE(B.COLUMN_NAME), '_', 2), '_', -1) 
                ELSE ' ' END AS CN2,
                CASE WHEN LENGTH(B.COLUMN_NAME) - LENGTH(REPLACE(B.COLUMN_NAME, '_', '')) + 1 >= 3 THEN 
                    SUBSTRING_INDEX(SUBSTRING_INDEX(LCASE(B.COLUMN_NAME), '_', 3), '_', -1) 
                ELSE ' ' END AS CN3,
                CASE WHEN LENGTH(B.COLUMN_NAME) - LENGTH(REPLACE(B.COLUMN_NAME, '_', '')) + 1 >= 4 THEN 
                    SUBSTRING_INDEX(SUBSTRING_INDEX(LCASE(B.COLUMN_NAME), '_', 4), '_', -1) 
                ELSE ' ' END AS CN4,
                CASE WHEN LENGTH(B.COLUMN_NAME) - LENGTH(REPLACE(B.COLUMN_NAME, '_', '')) + 1 >= 5 THEN 
                    SUBSTRING_INDEX(SUBSTRING_INDEX(LCASE(B.COLUMN_NAME), '_', 5), '_', -1) 
                ELSE ' ' END AS CN5,
              B.DATA_TYPE, 
              B.COLUMN_COMMENT
FROM information_schema.TABLES A
INNER JOIN information_schema.COLUMNS B ON (A.TABLE_NAME = B.TABLE_NAME) 
WHERE 	A.TABLE_NAME = '[테이블]'
AND A.TABLE_SCHEMA = '[디비]'
ORDER BY B.ORDINAL_POSITION
) A

_(언더바) 갯수만큼 CN1 2 3 4 5 추가