62 lines
3.0 KiB
C#
62 lines
3.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
|
|
namespace EShang.Common
|
|
{
|
|
/// <summary>
|
|
/// 数据表创建类
|
|
/// </summary>
|
|
public class CreateTableHelper
|
|
{
|
|
/// <summary>
|
|
/// 创建服务区分表
|
|
/// </summary>
|
|
/// <param name="oracleHelper">数据库连接类</param>
|
|
/// <param name="ownerName">表所有者名称</param>
|
|
/// <param name="tableName">主表名称</param>
|
|
/// <param name="indexNamePrefix">索引前缀</param>
|
|
/// <param name="serverPartCodeList">服务区编码列表</param>
|
|
/// <param name="tableIndexColumnList">索引字段列表</param>
|
|
/// <returns></returns>
|
|
public static bool CreateTable(OracleHelper oracleHelper, string ownerName, string tableName,
|
|
string indexNamePrefix, List<string> serverPartCodeList, List<string> tableIndexColumnList)
|
|
{
|
|
if (oracleHelper == null || string.IsNullOrWhiteSpace(ownerName) ||
|
|
string.IsNullOrWhiteSpace(tableName) || string.IsNullOrWhiteSpace(indexNamePrefix) ||
|
|
serverPartCodeList.Count == 0 || tableIndexColumnList.Count == 0)
|
|
{
|
|
return false;
|
|
}
|
|
System.Data.DataTable _ConstraintTable = oracleHelper.ExcuteSqlGetDataSet(
|
|
$@"SELECT A.OWNER,A.TABLE_NAME,A.CONSTRAINT_NAME,
|
|
CASE A.CONSTRAINT_TYPE WHEN 'P' THEN ' PRIMARY KEY (' ELSE ' UNIQUE (' END ||
|
|
WM_CONCAT(B.COLUMN_NAME) || ') USING INDEX' AS COLUMN_NAME
|
|
FROM SYS.ALL_CONSTRAINTS A,SYS.ALL_CONS_COLUMNS B
|
|
WHERE A.OWNER = B.OWNER AND A.TABLE_NAME = B.TABLE_NAME AND
|
|
A.CONSTRAINT_NAME = B.CONSTRAINT_NAME AND A.OWNER = '{ownerName}' AND
|
|
A.TABLE_NAME = '{tableName}' AND A.CONSTRAINT_TYPE IN ('P','U')
|
|
GROUP BY A.OWNER,A.TABLE_NAME,A.CONSTRAINT_NAME,A.CONSTRAINT_TYPE").Tables[0];
|
|
List<string> _SQLList = new List<string>();
|
|
foreach (string _strServerPartCode in serverPartCodeList)
|
|
{
|
|
_SQLList.Add($@"CREATE TABLE {ownerName}.{tableName}_{_strServerPartCode} AS
|
|
SELECT * FROM {ownerName}.{tableName} WHERE 1 = 2 ");
|
|
_SQLList.Add($@"CREATE INDEX {ownerName}.INDEX_{indexNamePrefix}_{_strServerPartCode}
|
|
ON {ownerName}.{tableName}_{_strServerPartCode} ( {string.Join(",", tableIndexColumnList.ToArray())} )");
|
|
foreach (System.Data.DataRow _DataRow in _ConstraintTable.Rows)
|
|
{
|
|
_SQLList.Add($@"ALTER TABLE {ownerName}.{tableName}_{_strServerPartCode}
|
|
ADD CONSTRAINT {_DataRow["CONSTRAINT_NAME"].ToString()}_{_strServerPartCode}
|
|
{_DataRow["COLUMN_NAME"].ToString()} ");
|
|
}
|
|
}
|
|
if (_SQLList.Count > 0)
|
|
{
|
|
oracleHelper.ExecuteSqlTran(_SQLList);
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
} |