DATA_GRIP_PARAMETERS = """
SELECT
  SPECIFIC_SCHEMA AS PROCEDURE_CAT,
  NULL AS `PROCEDURE_SCHEM`,
  SPECIFIC_NAME AS `PROCEDURE_NAME`,
  IFNULL(PARAMETER_NAME, '') AS `COLUMN_NAME`,
  CASE
    WHEN PARAMETER_MODE = 'IN'
    THEN 1
    WHEN PARAMETER_MODE = 'OUT'
    THEN 4
    WHEN PARAMETER_MODE = 'INOUT'
    THEN 2
    WHEN ORDINAL_POSITION = 0
    THEN 5
    ELSE 0
  END AS `COLUMN_TYPE`,
  CASE
    WHEN UPPER(DATA_TYPE) = 'DECIMAL'
    THEN 3
    WHEN UPPER(DATA_TYPE) = 'DECIMAL UNSIGNED'
    THEN 3
    WHEN UPPER(DATA_TYPE) = 'TINYINT'
    THEN -6
    WHEN UPPER(DATA_TYPE) = 'TINYINT UNSIGNED'
    THEN -6
    WHEN UPPER(DATA_TYPE) = 'BOOLEAN'
    THEN 16
    WHEN UPPER(DATA_TYPE) = 'SMALLINT'
    THEN 5
    WHEN UPPER(DATA_TYPE) = 'SMALLINT UNSIGNED'
    THEN 5
    WHEN UPPER(DATA_TYPE) = 'INT'
    THEN 4
    WHEN UPPER(DATA_TYPE) = 'INT UNSIGNED'
    THEN 4
    WHEN UPPER(DATA_TYPE) = 'FLOAT'
    THEN 7
    WHEN UPPER(DATA_TYPE) = 'FLOAT UNSIGNED'
    THEN 7
    WHEN UPPER(DATA_TYPE) = 'DOUBLE'
    THEN 8
    WHEN UPPER(DATA_TYPE) = 'DOUBLE UNSIGNED'
    THEN 8
    WHEN UPPER(DATA_TYPE) = 'NULL'
    THEN 0
    WHEN UPPER(DATA_TYPE) = 'TIMESTAMP'
    THEN 93
    WHEN UPPER(DATA_TYPE) = 'BIGINT'
    THEN -5
    WHEN UPPER(DATA_TYPE) = 'BIGINT UNSIGNED'
    THEN -5
    WHEN UPPER(DATA_TYPE) = 'MEDIUMINT'
    THEN 4
    WHEN UPPER(DATA_TYPE) = 'MEDIUMINT UNSIGNED'
    THEN 4
    WHEN UPPER(DATA_TYPE) = 'DATE'
    THEN 91
    WHEN UPPER(DATA_TYPE) = 'TIME'
    THEN 92
    WHEN UPPER(DATA_TYPE) = 'DATETIME'
    THEN 93
    WHEN UPPER(DATA_TYPE) = 'YEAR'
    THEN 91
    WHEN UPPER(DATA_TYPE) = 'VARCHAR'
    THEN 12
    WHEN UPPER(DATA_TYPE) = 'VARBINARY'
    THEN -3
    WHEN UPPER(DATA_TYPE) = 'BIT'
    THEN -7
    WHEN UPPER(DATA_TYPE) = 'JSON'
    THEN -1
    WHEN UPPER(DATA_TYPE) = 'ENUM'
    THEN 1
    WHEN UPPER(DATA_TYPE) = 'SET'
    THEN 1
    WHEN UPPER(DATA_TYPE) = 'TINYBLOB'
    THEN -3
    WHEN UPPER(DATA_TYPE) = 'TINYTEXT'
    THEN 12
    WHEN UPPER(DATA_TYPE) = 'MEDIUMBLOB'
    THEN -4
    WHEN UPPER(DATA_TYPE) = 'MEDIUMTEXT'
    THEN -1
    WHEN UPPER(DATA_TYPE) = 'LONGBLOB'
    THEN -4
    WHEN UPPER(DATA_TYPE) = 'LONGTEXT'
    THEN -1
    WHEN UPPER(DATA_TYPE) = 'BLOB'
    THEN -4
    WHEN UPPER(DATA_TYPE) = 'TEXT'
    THEN -1
    WHEN UPPER(DATA_TYPE) = 'CHAR'
    THEN 1
    WHEN UPPER(DATA_TYPE) = 'BINARY'
    THEN -2
    WHEN UPPER(DATA_TYPE) = 'GEOMETRY'
    THEN -2
    WHEN UPPER(DATA_TYPE) = 'UNKNOWN'
    THEN 1111
    WHEN UPPER(DATA_TYPE) = 'POINT'
    THEN -2
    WHEN UPPER(DATA_TYPE) = 'LINESTRING'
    THEN -2
    WHEN UPPER(DATA_TYPE) = 'POLYGON'
    THEN -2
    WHEN UPPER(DATA_TYPE) = 'MULTIPOINT'
    THEN -2
    WHEN UPPER(DATA_TYPE) = 'MULTILINESTRING'
    THEN -2
    WHEN UPPER(DATA_TYPE) = 'MULTIPOLYGON'
    THEN -2
    WHEN UPPER(DATA_TYPE) = 'GEOMETRYCOLLECTION'
    THEN -2
    WHEN UPPER(DATA_TYPE) = 'GEOMCOLLECTION'
    THEN -2
    ELSE 1111
  END AS `DATA_TYPE`,
  UPPER(
    CASE
      WHEN LOCATE('UNSIGNED', UPPER(DTD_IDENTIFIER)) <> 0
      AND LOCATE('UNSIGNED', UPPER(DATA_TYPE)) = 0
      AND LOCATE('SET', UPPER(DATA_TYPE)) <> 1
      AND LOCATE('ENUM', UPPER(DATA_TYPE)) <> 1
      THEN CONCAT(DATA_TYPE, ' UNSIGNED')
      WHEN UPPER(DATA_TYPE) = 'POINT'
      THEN 'GEOMETRY'
      WHEN UPPER(DATA_TYPE) = 'LINESTRING'
      THEN 'GEOMETRY'
      WHEN UPPER(DATA_TYPE) = 'POLYGON'
      THEN 'GEOMETRY'
      WHEN UPPER(DATA_TYPE) = 'MULTIPOINT'
      THEN 'GEOMETRY'
      WHEN UPPER(DATA_TYPE) = 'MULTILINESTRING'
      THEN 'GEOMETRY'
      WHEN UPPER(DATA_TYPE) = 'MULTIPOLYGON'
      THEN 'GEOMETRY'
      WHEN UPPER(DATA_TYPE) = 'GEOMETRYCOLLECTION'
      THEN 'GEOMETRY'
      WHEN UPPER(DATA_TYPE) = 'GEOMCOLLECTION'
      THEN 'GEOMETRY'
      ELSE UPPER(DATA_TYPE)
    END
  ) AS TYPE_NAME,
  CASE
    WHEN LCASE(DATA_TYPE) = 'date'
    THEN 0
    WHEN LCASE(DATA_TYPE) = 'time'
    OR LCASE(DATA_TYPE) = 'datetime'
    OR LCASE(DATA_TYPE) = 'timestamp'
    THEN DATETIME_PRECISION
    WHEN UPPER(DATA_TYPE) = 'MEDIUMINT' AND LOCATE('UNSIGNED', UPPER(DTD_IDENTIFIER)) <> 0
    THEN 8
    WHEN UPPER(DATA_TYPE) = 'JSON'
    THEN 1073741824
    ELSE NUMERIC_PRECISION
  END AS `PRECISION`,
  CASE
    WHEN LCASE(DATA_TYPE) = 'date'
    THEN 10
    WHEN LCASE(DATA_TYPE) = 'time'
    THEN 8 + (
      CASE
        WHEN DATETIME_PRECISION > 0
        THEN DATETIME_PRECISION + 1
        ELSE DATETIME_PRECISION
      END
    )
    WHEN LCASE(DATA_TYPE) = 'datetime' OR LCASE(DATA_TYPE) = 'timestamp'
    THEN 19 + (
      CASE
        WHEN DATETIME_PRECISION > 0
        THEN DATETIME_PRECISION + 1
        ELSE DATETIME_PRECISION
      END
    )
    WHEN UPPER(DATA_TYPE) = 'MEDIUMINT' AND LOCATE('UNSIGNED', UPPER(DTD_IDENTIFIER)) <> 0
    THEN 8
    WHEN UPPER(DATA_TYPE) = 'JSON'
    THEN 1073741824
    WHEN CHARACTER_MAXIMUM_LENGTH IS NULL
    THEN NUMERIC_PRECISION
    WHEN CHARACTER_MAXIMUM_LENGTH > 2147483647
    THEN 2147483647
    ELSE CHARACTER_MAXIMUM_LENGTH
  END AS LENGTH,
  NUMERIC_SCALE AS `SCALE`,
  10 AS RADIX,
  1 AS `NULLABLE`,
  NULL AS `REMARKS`,
  NULL AS `COLUMN_DEF`,
  NULL AS `SQL_DATA_TYPE`,
  NULL AS `SQL_DATETIME_SUB`,
  CASE
    WHEN CHARACTER_OCTET_LENGTH > 2147483647
    THEN 2147483647
    ELSE CHARACTER_OCTET_LENGTH
  END AS `CHAR_OCTET_LENGTH`,
  ORDINAL_POSITION,
  'YES' AS `IS_NULLABLE`,
  SPECIFIC_NAME
FROM INFORMATION_SCHEMA.PARAMETERS
WHERE
  SPECIFIC_SCHEMA = 'magic' AND SPECIFIC_NAME LIKE '%'
ORDER BY
  SPECIFIC_SCHEMA,
  SPECIFIC_NAME,
  ROUTINE_TYPE,
  ORDINAL_POSITION
"""

DATA_GRIP_VARIABLES = """
/* mysql-connector-java-8.0.25 (Revision: 08be9e9b4cba6aa115f9b27b215887af40b159e0) */SELECT
   @@session.auto_increment_increment AS auto_increment_increment,
   @@character_set_client AS character_set_client,
   @@character_set_connection AS character_set_connection,
   @@character_set_results AS character_set_results,
   @@character_set_server AS character_set_server,
   @@collation_server AS collation_server,
   @@collation_connection AS collation_connection,
   @@init_connect AS init_connect,
   @@interactive_timeout AS interactive_timeout,
   @@license AS license,
   @@lower_case_table_names AS lower_case_table_names,
   @@max_allowed_packet AS max_allowed_packet,
   @@net_write_timeout AS net_write_timeout,
   @@performance_schema AS performance_schema,
   @@sql_mode AS sql_mode,
   @@system_time_zone AS system_time_zone,
   @@time_zone AS time_zone,
   @@transaction_isolation AS transaction_isolation,
   @@wait_timeout AS wait_timeout
"""

DATA_GRIP_TABLES = """
/* ApplicationName=DataGrip 2022.2.5 */
SELECT
  TABLE_SCHEMA AS TABLE_CAT,
  NULL AS TABLE_SCHEM,
  TABLE_NAME,
  CASE
    WHEN TABLE_TYPE = 'BASE TABLE'
    THEN CASE
      WHEN TABLE_SCHEMA = 'mysql' OR TABLE_SCHEMA = 'performance_schema'
      THEN 'SYSTEM TABLE'
      ELSE 'TABLE'
    END
    WHEN TABLE_TYPE = 'TEMPORARY'
    THEN 'LOCAL_TEMPORARY'
    ELSE TABLE_TYPE
  END AS TABLE_TYPE,
  TABLE_COMMENT AS REMARKS,
  NULL AS TYPE_CAT,
  NULL AS TYPE_SCHEM,
  NULL AS TYPE_NAME,
  NULL AS SELF_REFERENCING_COL_NAME,
  NULL AS REF_GENERATION
FROM INFORMATION_SCHEMA.TABLES
WHERE
  TABLE_SCHEMA = 'db' AND TABLE_NAME LIKE '%'
HAVING
  TABLE_TYPE IN ('LOCAL TEMPORARY', 'SYSTEM TABLE', 'SYSTEM VIEW', 'TABLE', 'VIEW')
ORDER BY
  TABLE_TYPE,
  TABLE_SCHEMA,
  TABLE_NAME
"""

TABLEAU_INDEXES_1 = """
SELECT
  A.REFERENCED_TABLE_SCHEMA AS PKTABLE_CAT,
  NULL AS PKTABLE_SCHEM,
  A.REFERENCED_TABLE_NAME AS PKTABLE_NAME,
  A.REFERENCED_COLUMN_NAME AS PKCOLUMN_NAME,
  A.TABLE_SCHEMA AS FKTABLE_CAT,
  NULL AS FKTABLE_SCHEM,
  A.TABLE_NAME AS FKTABLE_NAME,
  A.COLUMN_NAME AS FKCOLUMN_NAME,
  A.ORDINAL_POSITION AS KEY_SEQ,
  CASE
    WHEN R.UPDATE_RULE = 'CASCADE' THEN 0 
    WHEN R.UPDATE_RULE = 'SET NULL' THEN 2 
    WHEN R.UPDATE_RULE = 'SET DEFAULT' THEN 4 
    WHEN R.UPDATE_RULE = 'SET RESTRICT' THEN 1 
    WHEN R.UPDATE_RULE = 'SET NO ACTION' THEN 3 
    ELSE 3 
  END AS UPDATE_RULE,
  CASE
    WHEN R.DELETE_RULE = 'CASCADE' THEN 0 
    WHEN R.DELETE_RULE = 'SET NULL' THEN 2
    WHEN R.DELETE_RULE = 'SET DEFAULT' THEN 4
    WHEN R.DELETE_RULE = 'SET RESTRICT' THEN 1 
    WHEN R.DELETE_RULE = 'SET NO ACTION' THEN 3 
  ELSE 3 END AS DELETE_RULE,
  A.CONSTRAINT_NAME AS FK_NAME,
  'PRIMARY' AS PK_NAME,
  7 AS DEFERRABILITY 
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE A 
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE D 
ON (
  D.TABLE_SCHEMA=A.REFERENCED_TABLE_SCHEMA 
  AND D.TABLE_NAME=A.REFERENCED_TABLE_NAME 
  AND D.COLUMN_NAME=A.REFERENCED_COLUMN_NAME) 
JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS R 
ON (
  R.CONSTRAINT_NAME = A.CONSTRAINT_NAME 
  AND R.TABLE_NAME = A.TABLE_NAME 
  AND R.CONSTRAINT_SCHEMA = A.TABLE_SCHEMA) 
WHERE 
  D.CONSTRAINT_NAME='PRIMARY' 
  AND A.TABLE_SCHEMA = 'magic' 
ORDER BY FKTABLE_CAT, FKTABLE_NAME, KEY_SEQ, PKTABLE_NAME
"""

TABLEAU_INDEXES_2 = """
SELECT COLUMN_NAME,
 REFERENCED_TABLE_NAME,
 REFERENCED_COLUMN_NAME,
 ORDINAL_POSITION,
 CONSTRAINT_NAME
FROM information_schema.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_NAME IS NOT NULL
 AND TABLE_NAME = 'x'
 AND TABLE_SCHEMA = 'db'
"""

TABLEAU_COLUMNS = """
SELECT `table_name`, `column_name`
FROM `information_schema`.`columns`
WHERE `data_type`='enum' AND `table_schema`='db'
"""

TABLEAU_TABLES = """
SELECT TABLE_NAME,TABLE_COMMENT,IF(TABLE_TYPE='BASE TABLE', 'TABLE', TABLE_TYPE),TABLE_SCHEMA 
FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'db' AND ( TABLE_TYPE='BASE TABLE' OR TABLE_TYPE='VIEW' )  
ORDER BY TABLE_SCHEMA, TABLE_NAME
"""

TABLEAU_SCHEMATA = """
SELECT NULL, NULL, NULL, SCHEMA_NAME
FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME LIKE '%'
ORDER BY SCHEMA_NAME
"""
