private async Task <string> GetTableHasRowsQueryAsyncCore(Identifier tableName)
        {
            var quotedTableName = Dialect.QuoteName(Identifier.CreateQualifiedIdentifier(tableName.Schema, tableName.LocalName));
            var filterSql       = "select 1 as dummy_col from " + quotedTableName;
            var sql             = $"select case when exists ({ filterSql }) then 1 else 0 end as dummy";

            var suffix = await _fromQuerySuffixAsync.ConfigureAwait(false);

            return(suffix.IsNullOrWhiteSpace()
                ? sql
                : sql + " from " + suffix);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:Ceen.Database.ColumnMapping"/> class.
        /// </summary>
        /// <param name="dialect">The database dialect.</param>
        /// <param name="member">The member to map.</param>
        /// <param name="memberType">The member type</param>
        private ColumnMapping(IDatabaseDialect dialect, MemberInfo member, Type memberType)
        {
            Member           = member ?? throw new ArgumentNullException(nameof(member));
            MemberType       = memberType;
            ColumnName       = dialect.GetName(member);
            QuotedColumnName = dialect.QuoteName(ColumnName);
            IsPrimaryKey     = member.GetCustomAttributes <PrimaryKeyAttribute>(true).Any();
            var sqlType = dialect.GetSqlColumnType(member);

            SqlType            = sqlType.Item1;
            AutoGenerateAction = sqlType.Item2;
            ValidationRules    = member.GetCustomAttributes <ValidationBaseAttribute>(true).ToArray();
        }
Exemplo n.º 3
0
        public static Task <ulong> GetRowCountAsync(this IDbConnectionFactory connection, IDatabaseDialect dialect, Identifier tableName, CancellationToken cancellationToken)
        {
            if (connection == null)
            {
                throw new ArgumentNullException(nameof(connection));
            }
            if (dialect == null)
            {
                throw new ArgumentNullException(nameof(dialect));
            }
            if (tableName == null)
            {
                throw new ArgumentNullException(nameof(tableName));
            }

            var name       = Identifier.CreateQualifiedIdentifier(tableName.Schema, tableName.LocalName);
            var quotedName = dialect.QuoteName(name);

            var query = "SELECT COUNT(*) FROM " + quotedName;

            return(connection.ExecuteScalarAsync <ulong>(query, cancellationToken));
        }
Exemplo n.º 4
0
 /// <summary>
 /// Returns the column name in quotes
 /// </summary>
 /// <param name="propertyname">The name of the property to get the column name for</param>
 /// <returns>The quoted column name</returns>
 public string QuotedColumnName(string propertyname)
 {
     return(Dialect.QuoteName(AllColumnsByMemberName[propertyname].ColumnName));
 }
Exemplo n.º 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:Ceen.Database.TableMapping"/> class.
        /// </summary>
        /// <param name="dialect">The database dialect.</param>
        /// <param name="type">The type to mape.</param>
        /// <param name="nameoverride">An optional name override for the table name</param>
        /// <param name="ignorefields">An optional flag that removes all fields from the map</param>
        public TableMapping(IDatabaseDialect dialect, Type type, string nameoverride = null, bool ignoreFields = false)
        {
            Dialect         = dialect ?? throw new ArgumentNullException(nameof(dialect));
            Type            = type;
            Name            = nameoverride ?? dialect.GetName(type);
            QuotedTableName = dialect.QuoteName(Name);
            AllColumns      = type
                              .GetProperties()
                              .Where(x => !x.GetCustomAttributes <IgnoreAttribute>(true).Any())
                              .Select(x => new ColumnMapping(dialect, x))
                              .Concat(
                type
                .GetFields()
                .Where(x => !x.GetCustomAttributes <IgnoreAttribute>(true).Any() && !ignoreFields)
                .Select(x => new ColumnMapping(dialect, x))
                )
                              .ToArray();

            var dbcreated = new[] { AutoGenerateAction.DatabaseAutoID, AutoGenerateAction.DatabaseChangeTimestamp, AutoGenerateAction.DatabaseCreateTimestamp };

            InsertColumns            = AllColumns.Where(x => !dbcreated.Contains(x.AutoGenerateAction)).ToArray();
            ColumnsWithoutPrimaryKey = AllColumns.Where(x => !x.IsPrimaryKey).ToArray();
            UpdateColumns            = ColumnsWithoutPrimaryKey.Where(x => !dbcreated.Contains(x.AutoGenerateAction)).ToArray();

            PrimaryKeys            = AllColumns.Where(x => x.IsPrimaryKey).ToArray();
            AllColumnsBySqlName    = AllColumns.ToDictionary(x => x.ColumnName, x => x);
            AllColumnsByMemberName = AllColumns.ToDictionary(x => x.Member.Name, x => x);

            // Build the unique maps
            var uniques = AllColumns
                          .Select(x => new Tuple <ColumnMapping, string[]>(x, x.Member.GetCustomAttributes <UniqueAttribute>().Select(y => y.Group).ToArray()))
                          .Where(x => x.Item2 != null && x.Item2.Length > 0);

            var solos  = uniques.Where(x => x.Item2.Contains(null));
            var groups = uniques
                         .SelectMany(x =>
                                     x.Item2
                                     .Where(y => y != null)
                                     .Select(y => new KeyValuePair <string, ColumnMapping>(y, x.Item1))
                                     )
                         .GroupBy(x => x.Key);

            Uniques =
                solos
                .Select(x => new UniqueMapping(null, new ColumnMapping[] { x.Item1 }))
                .Concat(
                    groups.Select(x => new UniqueMapping(x.Key, x.Select(y => y.Value).ToArray()))
                    ).ToArray();

            ClientGenerated = new HashSet <ColumnMapping>(AllColumns
                                                          .Where(x =>
                                                                 x.AutoGenerateAction == AutoGenerateAction.ClientGenerateGuid ||
                                                                 x.AutoGenerateAction == AutoGenerateAction.ClientChangeTimestamp ||
                                                                 x.AutoGenerateAction == AutoGenerateAction.ClientCreateTimestamp
                                                                 )
                                                          );

            DatabaseGenerated = new HashSet <ColumnMapping>(AllColumns
                                                            .Where(x =>
                                                                   x.AutoGenerateAction == AutoGenerateAction.DatabaseAutoID ||
                                                                   x.AutoGenerateAction == AutoGenerateAction.DatabaseChangeTimestamp ||
                                                                   x.AutoGenerateAction == AutoGenerateAction.DatabaseCreateTimestamp
                                                                   )
                                                            );

            IsPrimaryKeyAutogenerated           = PrimaryKeys.Any(x => x.AutoGenerateAction == AutoGenerateAction.DatabaseAutoID);
            HasDatabaseGeneratedCreateTimestamp = ColumnsWithoutPrimaryKey.Any(x => x.AutoGenerateAction == AutoGenerateAction.DatabaseCreateTimestamp);
            HasDatabaseGeneratedChangeTimestamp = ColumnsWithoutPrimaryKey.Any(x => x.AutoGenerateAction == AutoGenerateAction.DatabaseChangeTimestamp);
            ValidationRules = type.GetCustomAttributes <ValidationBaseAttribute>(true).ToArray();
            AllColumnsWithValidationRules = AllColumns.Where(x => x.ValidationRules.Length > 0).ToArray();
            HasValidationRules            = ValidationRules.Length + AllColumnsWithValidationRules.Length > 0;
        }