Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ORM框架支持创建索引 #104

Open
tjliuqian opened this issue Sep 5, 2017 · 0 comments
Open

ORM框架支持创建索引 #104

tjliuqian opened this issue Sep 5, 2017 · 0 comments

Comments

@tjliuqian
Copy link

建议在Rafy中加入以下创建索引的功能支持

    protected void AddIndex(ConcreteProperty property)
    {
        string sql = string.Empty;
        string indexName = string.Empty;

        var entityType = property.Owner;
        var meta = Rafy.MetaModel.CommonModel.Entities.Find(entityType);
        var tableName = meta.TableMeta.TableName;
        var columnMeta = meta.Property(property.Property).ColumnMeta;
        var columnName = columnMeta.ColumnName ?? property.Name;
        indexName = string.Format(@"IDX_{0}_{1}", tableName.ToUpper(), columnName.ToUpper());
        indexName = indexName.Length > 30 ? indexName.Substring(0, 30) : indexName;

        var dbSetting = Rafy.Data.DbSetting.FindOrCreate(this.DbSetting);
        if (Rafy.Data.DbSetting.IsOracleProvider(dbSetting))
        {
            sql = string.Format(

@"DECLARE indexCount NUMBER(10):=0;createStr VARCHAR2(512);
BEGIN
SELECT COUNT(*) INTO indexCount
FROM USER_INDEXES
WHERE TABLE_NAME='{1}'
AND INDEX_NAME ='{0}';
IF indexCount=0 THEN
createStr:='CREATE INDEX {0} ON {1}({2})';
EXECUTE IMMEDIATE createStr;
END IF;
END;", indexName.ToUpper(), tableName.ToUpper(), columnName.ToUpper());
}
else
{
sql = string.Format(
@"IF NOT EXISTS
(
SELECT *
FROM SYSINDEXES
WHERE ID=OBJECT_ID('{1}')
AND NAME='{0}'
)
CREATE INDEX {0} ON {1}({2})", indexName.ToUpper(), tableName.ToUpper(), columnName.ToUpper());
}

        this.RunSql(sql);
    }

    protected void AddIndexExt(Type owner,string colume)
    {
        string sql = string.Empty;
        string indexName = string.Empty;

        var entityType = owner;
        var meta = Rafy.MetaModel.CommonModel.Entities.Find(entityType);
        var tableName = meta.TableMeta.TableName;
    
        var columnName = colume;
        indexName = string.Format(@"IDX_{0}_{1}", tableName.ToUpper(), columnName.ToUpper());
        indexName = indexName.Length > 30 ? indexName.Substring(0, 30) : indexName;

        var dbSetting = Rafy.Data.DbSetting.FindOrCreate(this.DbSetting);
        if (Rafy.Data.DbSetting.IsOracleProvider(dbSetting))
        {
            sql = string.Format(

@"DECLARE indexCount NUMBER(10):=0;createStr VARCHAR2(512);
BEGIN
SELECT COUNT(*) INTO indexCount
FROM USER_INDEXES
WHERE TABLE_NAME='{1}'
AND INDEX_NAME ='{0}';
IF indexCount=0 THEN
createStr:='CREATE INDEX {0} ON {1}({2})';
EXECUTE IMMEDIATE createStr;
END IF;
END;", indexName.ToUpper(), tableName.ToUpper(), columnName.ToUpper());
}
else
{
sql = string.Format(
@"IF NOT EXISTS
(
SELECT name FROM sys.indexes
WHERE name = N'{0}'
)
CREATE INDEX {0} ON {1}({2})", indexName.ToUpper(), tableName.ToUpper(), columnName.ToUpper());
}

        this.RunSql(sql);
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants