Exemplo n.º 1
0
        public static string GetCreateSql(this TableMapping.PrimaryKeyDefinition primaryKey)
        {
            var sb = new StringBuilder();

            if (!primaryKey.Name.IsNullOrWhitespace())
            {
                sb.Append("CONSTRAINT ");
                sb.Append(primaryKey.Name);
                sb.AppendLine();
            }
            sb.Append("PRIMARY KEY (");
            bool first = true;

            foreach (var column in primaryKey.Columns)
            {
                if (!first)
                {
                    sb.Append(", ");
                }
                sb.Append(Quote(column.Name));
                if (column.PrimaryKey.Direction != Direction.Default)
                {
                    sb.Append(" ");
                    sb.Append(column.PrimaryKey.Direction);
                }
                first = false;
            }
            sb.Append(")");
            return(sb.ToString());
        }
Exemplo n.º 2
0
        internal static TableMapping.PrimaryKeyDefinition GetPrimaryKey(
            IEnumerable <TableMapping.Column> columns,
            out TableMapping.Column autoIncCol)
        {
            TableMapping.PrimaryKeyDefinition primaryKey = null;
            autoIncCol = null;

            var pkCols = columns.Where(c => c.PrimaryKey != null).OrderBy(c => c.PrimaryKey.Order).ToArray();

            if (pkCols.Any())
            {
                primaryKey = new TableMapping.PrimaryKeyDefinition
                {
                    Columns = pkCols,
                    Name    = GetPrimaryKeyName(pkCols)
                };
                var autoInc = primaryKey.Columns.Where(c => c.IsAutoIncrement).ToArray();
                if (autoInc.Count() > 1)
                {
                    throw new ArgumentException("Only one property can be an auto incrementing primary key");
                }
                autoIncCol = autoInc.FirstOrDefault();
            }

            return(primaryKey);
        }