<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.thinkgem.jeesite.modules.gen.dao.GenDataBaseDictDao">
    
	<select id="findTableList" resultType="GenTable">
		<if test="dbName == 'oracle'">
			SELECT 
				t.TABLE_NAME AS name, 
				c.COMMENTS AS comments
			FROM user_tables t, user_tab_comments c 
			WHERE t.table_name = c.table_name
			<if test="name != null and name != ''">
				AND t.TABLE_NAME = upper(#{name})
			</if>
			ORDER BY t.TABLE_NAME
		</if>
		<if test="dbName == 'mssql'">
			SELECT t.name AS name,b.value AS comments 
			FROM sys.objects t LEFT JOIN sys.extended_properties b ON b.major_id=t.object_id and b.minor_id=0 and b.class=1 AND b.name='MS_Description'
			WHERE t.type='U'
			<if test="name != null and name != ''">
				AND t.name = (#{name})
			</if>
			ORDER BY t.name
		</if>
		<if test="dbName == 'mysql'">			
			SELECT t.table_name AS name,t.TABLE_COMMENT AS comments 
			FROM information_schema.`TABLES` t 
			WHERE t.TABLE_SCHEMA = (select database())
			<if test="name != null and name != ''">
				AND t.TABLE_NAME = (#{name})
			</if>
			ORDER BY t.TABLE_NAME
		</if>
	</select>
	
	<select id="findTableColumnList" resultType="GenTableColumn">
		<if test="dbName == 'oracle'">
			SELECT
				t.COLUMN_NAME AS name,<!-- 
				t.DATA_TYPE,
				t.DATA_LENGTH,
				t.DATA_PRECISION,
				t.DATA_SCALE, -->
				(CASE WHEN t.NULLABLE = 'Y' THEN '1' ELSE '0' END) AS isNull,
				(t.COLUMN_ID * 10) AS sort,
				c.COMMENTS AS comments,
				decode(t.DATA_TYPE,'DATE',t.DATA_TYPE || '(' || t.DATA_LENGTH || ')',
					'VARCHAR2', t.DATA_TYPE || '(' || t.DATA_LENGTH || ')',
					'VARCHAR', t.DATA_TYPE || '(' || t.DATA_LENGTH || ')',
					'NVARCHAR2', t.DATA_TYPE || '(' || t.DATA_LENGTH/2 || ')',
					'CHAR', t.DATA_TYPE || '(' || t.DATA_LENGTH || ')',
					'NUMBER',t.DATA_TYPE || (nvl2(t.DATA_PRECISION,nvl2(decode(t.DATA_SCALE,0,null,t.DATA_SCALE),
						'(' || t.DATA_PRECISION || ',' || t.DATA_SCALE || ')', 
						'(' || t.DATA_PRECISION || ')'),'(18)')),t.DATA_TYPE) AS jdbcType 
			FROM user_tab_columns t, user_col_comments c 
			WHERE t.TABLE_NAME = c.table_name 
				AND t.COLUMN_NAME = c.column_name 
			<if test="name != null and name != ''">
				AND t.TABLE_NAME = upper(#{name})
			</if>
			ORDER BY t.COLUMN_ID
		</if>
		<if test="dbName == 'mssql'">	
			SELECT t.COLUMN_NAME AS name, (CASE WHEN t.IS_NULLABLE = 'YES' THEN '1' ELSE '0' END) AS isNull,
				(t.ORDINAL_POSITION * 10) AS sort,isnull(g.[value], '') AS comments,
				(t.DATA_TYPE+
					CASE WHEN t.DATA_TYPE IN ('varchar','char','nvarchar','nchar') THEN '('+CONVERT(VARCHAR,t.CHARACTER_MAXIMUM_LENGTH)+')'
						WHEN t.DATA_TYPE IN ('numeric','decimal') THEN '('+CONVERT(VARCHAR,t.NUMERIC_PRECISION_RADIX)+','+CONVERT(VARCHAR,ISNULL(t.NUMERIC_SCALE, 0))+')'
					ELSE '' END) AS jdbcType 
			FROM INFORMATION_SCHEMA.COLUMNS t INNER JOIN sys.sysobjects o ON t.TABLE_NAME=o.name AND SCHEMA_NAME(o.uid)=t.TABLE_SCHEMA
				LEFT JOIN sys.extended_properties g ON o.id = g.major_id
					AND t.ORDINAL_POSITION = g.minor_id AND g.name='MS_Description'
			WHERE t.TABLE_SCHEMA = (SCHEMA_NAME())
			<if test="name != null and name != ''">
				AND t.TABLE_NAME = (#{name})
			</if>
			ORDER BY t.ORDINAL_POSITION
		</if>
		<if test="dbName == 'mysql'">	
			SELECT t.COLUMN_NAME AS name, (CASE WHEN t.IS_NULLABLE = 'YES' THEN '1' ELSE '0' END) AS isNull,
				(t.ORDINAL_POSITION * 10) AS sort,t.COLUMN_COMMENT AS comments,t.COLUMN_TYPE AS jdbcType 
			FROM information_schema.`COLUMNS` t 
			WHERE t.TABLE_SCHEMA = (select database())
			<if test="name != null and name != ''">
				AND t.TABLE_NAME = (#{name})
			</if>
			ORDER BY t.ORDINAL_POSITION
		</if>
	</select>
	
	<select id="findTablePK" resultType="string">
		<if test="dbName == 'oracle'">
			SELECT lower(cu.COLUMN_NAME) AS columnName
			FROM user_cons_columns cu, user_constraints au
			WHERE cu.constraint_name = au.constraint_name
				AND au.constraint_type = 'P'
				AND au.table_name = upper(#{name})
		</if>
		<if test="dbName == 'mssql'">
			SELECT lower(au.COLUMN_NAME) AS columnName 
			FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE au
			WHERE au.TABLE_NAME = (#{name})
			AND au.TABLE_SCHEMA = (SCHEMA_NAME())
		</if>
		<if test="dbName == 'mysql'">
			SELECT lower(au.COLUMN_NAME) AS columnName 
			FROM information_schema.`COLUMNS` au
			WHERE au.TABLE_SCHEMA = (select database()) 
			AND au.COLUMN_KEY='PRI' AND au.TABLE_NAME = (#{name})
		</if>
	</select>
	
</mapper>