OrmLiteConfig.DialectProvider determination at runtime?

Were writing an extension method that tests if an index exists. We’re using MySql in some places and Sqllite in others.

The problem is two projects reference it but each one only has MySQL or SqlLite loaded at runtime.

So this code throws an error Could not load file or assembly 'ServiceStack.OrmLite.MySql

    public static bool DoesIndexExist<T>(this IDbConnection db, string indexName)
    {
        if (OrmLiteConfig.DialectProvider == MySqlDialect.Provider)
        {
            var tableName = GetTableName<T>(db);
            var indexes = db.Query($"show index from {tableName}");
            return Enumerable.Any<dynamic>(indexes, x => x.Key_name == indexName);
        }
        if (OrmLiteConfig.DialectProvider == SqliteDialect.Provider)
        {
            // PRAGMA index_list(YourTable)
            // PRAGMA index_info(IndexName)
            var tableName = GetTableName<T>(db);
            var indexes = db.Query($"PRAGMA index_list({tableName})");
            return Enumerable.Any<dynamic>(indexes, x => x.Key_name == indexName);
        }
        throw new CDException("Invalid database in: DoesIndexExist");
    }

BTW, Don’t know if the Sqlite syntax works have not run this yet.

Also, will have the same problem with this method.

   public static int CreateCompositeIndex<T>(this IDbConnection db, string indexName,
        params Expression<Func<T, object>>[] columns)
    {
        var tableName = GetTableName<T>(db);
        var colNames = new List<string>();
        foreach (var col in columns)
        {
            switch (col.Body)
            {
                case MemberExpression mem:
                    colNames.Add(mem.Member.Name);
                    break;
                case UnaryExpression un:
                    if (un.Operand is MemberExpression mem2)
                    {
                        colNames.Add(mem2.Member.Name);
                    }
                    break;
            }
        }
        return db.ExecuteSql($"CREATE UNIQUE INDEX {indexName} on {tableName} ({colNames.Join(", ")})");
    }

Q: Do you have built-in support for these extension methods??

BTW, This logic seems to work for Sqlite.

           var indexes = db.Query($"PRAGMA index_list({tableName})");
            return Enumerable.Any<dynamic>(indexes, x => x.Name == indexName);

It’s not clear what the Issue is, you can have multiple Dialects in the same project, for example the ServiceStack.OrmLite.Tests project has all of them.

But if you do have a library that references the concrete Dialect Providers, that library always needs references to the concrete providers. If you don’t want the concrete dependency check against something that doesn’t require a concrete dependency like OrmLiteConfig.DialectProvider.GetType().Name instead.

OrmLite does have a [CompositeIndex] attribute which you can annotate on your Table classes to create composite indexes.

CompositeIndex thats what we needed. And there is an overload for unique.

So the rest of that stuff is not needed now.

Many thanks!

1 Like