Beispiel #1
0
        internal string Delete(BaseModel entity)
        {
            if (entity == null)
            {
                throw new ArgumentException("The entity is null, cannot delete a null entity", nameof(entity));
            }

            var type          = entity.GetType();
            var keyProperties = Cache.KeyPropertiesCache(type).ToList();

            if (!keyProperties.Any())
            {
                throw new ArgumentException("Entity must have at least one [Key] property");
            }

            var name = Cache.GetTableName(type);

            var sb = new StringBuilder();

            sb.Append($"delete from {name} where ");

            for (var i = 0; i < keyProperties.Count; i++)
            {
                var property = keyProperties.ElementAt(i);
                sb.Append($"{_strategy.Enclose(property.Name)} = @{property.Name}");
                if (i < keyProperties.Count - 1)
                {
                    sb.Append(" and ");
                }
            }

            return(sb.ToString());
        }
        internal static (string name, string columnList, string parametersList) GetInsertParameters(BaseModel entityToInsert, QueryStrategy strategy)
        {
            var type = entityToInsert.GetType();

            var name = GetTableName(type);

            var sbColumnList = new StringBuilder(null);

            var allProperties                = TypePropertiesCache(type).ToList();
            var identityProperties           = IdentityPropertiesCache(type).ToList();
            var allPropertiesExceptIndentity = allProperties.Except(identityProperties).ToList();

            var index = 0;
            var lst   = allPropertiesExceptIndentity;

            foreach (var property in lst)
            {
                sbColumnList.Append(strategy.Enclose(property.Name));
                if (index < lst.Count - 1)
                {
                    sbColumnList.Append(", ");
                }
                index++;
            }

            index = 0;
            var sbParameterList = new StringBuilder(null);

            foreach (var property in lst)
            {
                sbParameterList.Append($"@{property.Name}");
                if (index < lst.Count - 1)
                {
                    sbParameterList.Append(", ");
                }
                index++;
            }

            return(name, sbColumnList.ToString(), sbParameterList.ToString());
        }