Example #1
0
        internal static IList <TableMapping.Index> GetIndexes(Type info, PropertyInfo[] properties)
        {
            var indices = new List <TableMapping.Index>();

            IndexAttribute tblAtt = info.GetAttributes <IndexAttribute>().FirstOrDefault();

            if (tblAtt != null)
            {
                if (indices.Any(i => i.IndexName == tblAtt.Name))
                {
                    throw new NotSupportedException("Only one index attribute per index allowed on a table class.");
                }

                indices.Add(new TableMapping.Index(tblAtt.Name)
                {
                    Unique = tblAtt.Unique
                });
            }

            foreach (PropertyInfo prop in properties)
            {
                foreach (IndexedAttribute att in prop.GetAttributes <IndexedAttribute>())
                {
                    TableMapping.Index index = indices.FirstOrDefault(x => x.IndexName == att.Name);
                    if (index == null)
                    {
                        index = new TableMapping.Index(att.Name);
                        indices.Add(index);
                    }

                    var indexedCol = new TableMapping.Index.IndexedColumn(GetColumnName(prop))
                    {
                        Order     = att.Order,
                        Collation = att.Collation,
                        Direction = att.Direction
                    };
                    index.Columns.Add(indexedCol);
                }
            }

            return(indices);
        }
Example #2
0
        public static string GetCreateSql(this TableMapping.Index index, string tableName)
        {
            var sb = new StringBuilder();

            sb.Append("CREATE ");
            if (index.Unique)
            {
                sb.Append("UNIQUE ");
            }
            sb.Append("INDEX ");
            sb.Append(Quote(index.IndexName));
            sb.Append(" on ");
            sb.Append(Quote(tableName));
            sb.AppendLine(" (");
            bool first = true;

            foreach (var column in index.Columns.OrderBy(c => c.Order))
            {
                if (!first)
                {
                    sb.AppendLine(",");
                }
                sb.Append(Quote(column.ColumnName));
                if (column.Collation != Collation.Default)
                {
                    sb.Append(" COLLATE ");
                    sb.Append(column.Collation);
                }
                if (column.Direction != Direction.Default)
                {
                    sb.Append(" ");
                    sb.Append(column.Direction);
                }
                first = false;
            }
            sb.AppendLine();
            sb.Append(");");
            return(sb.ToString());
        }