Azavea.Open.DAO.SQL.AbstractSqlConnectionDescriptor.MakeCreateIndexCommand C# (CSharp) Method

MakeCreateIndexCommand() public method

Returns the SQL statement to create an index on a table. There may be database-specific additional keywords required (such as "COMPUTE STATISTICS"). The default implementation returns a simple standard-sql create index statement.
public MakeCreateIndexCommand ( string indexName, bool isUnique, string tableName, IEnumerable columnNames ) : string
indexName string Name of the index to create.
isUnique bool Is this a unique index?
tableName string What table to create the index on.
columnNames IEnumerable The columns included in the index.
return string
        public virtual string MakeCreateIndexCommand(string indexName,
            bool isUnique, string tableName,
            IEnumerable<string> columnNames)
        {
            StringBuilder sql = DbCaches.StringBuilders.Get();
            sql.Append("CREATE ");
            if (isUnique)
            {
                sql.Append("UNIQUE ");
            }
            sql.Append("INDEX ");
            sql.Append(indexName);
            sql.Append(" ON ");
            sql.Append(tableName);
            sql.Append(" (");
            bool first = true;
            foreach (string colName in columnNames)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    sql.Append(",");
                }
                sql.Append(colName);
            }
            sql.Append(") ");
            string retVal = sql.ToString();
            DbCaches.StringBuilders.Return(sql);
            return retVal;
        }

Usage Example

 /// <summary>
 /// Creates an index on a database table.
 /// </summary>
 /// <param name="connDesc">The database connection descriptor.  This is used both as
 ///                        a key for caching connections/commands as well as for
 ///                        getting the actual database connection the first time.</param>
 /// <param name="indexName">Name of the index to create.</param>
 /// <param name="isUnique">Is this a unique index?</param>
 /// <param name="tableName">What table to create the index on.</param>
 /// <param name="columnNames">The columns included in the index.</param>
 public static void CreateIndex(AbstractSqlConnectionDescriptor connDesc, string indexName, bool isUnique, string tableName,
     IEnumerable<string> columnNames)
 {
     string sql = connDesc.MakeCreateIndexCommand(indexName, isUnique, tableName, columnNames);
     XSafeCommand(connDesc, sql, null);
 }