Esempio n. 1
0
        private static string GenerateInsertSql(Type entityType, DapperRuntime runtime, DapperMetadata metadata)
        {
            var    fields  = metadata.Fields.Where(f => f.Ignore == false && f.AutoGeneration == false).OrderBy(x => x.Field.Name).ToArray();
            string columns = fields.Select(k => runtime.DelimitIdentifier(entityType, k.Name)).ToArrayString(", ");

            string parameters = fields.Select(k => $"{runtime.DelimitParameter(entityType, k.Field.Name)}").ToArrayString(", ");

            StringBuilder sqlBuilder = new StringBuilder();

            sqlBuilder.Append("INSERT INTO ");
            sqlBuilder.Append($"{runtime.DelimitIdentifier(entityType, metadata.TableName)} ");
            sqlBuilder.Append($"({columns}) ");
            sqlBuilder.Append(" VALUES ");
            sqlBuilder.Append($"({parameters})");

            return(sqlBuilder.ToString());
        }
Esempio n. 2
0
        private static string GenerateUpdateSql(Type entityType, DapperRuntime runtime, DapperMetadata metadata)
        {
            var    fields        = metadata.Fields.Where(f => !f.IsKey && !f.AutoGeneration && !f.Ignore).OrderBy(x => x.Field.Name).ToArray();
            string columnsSetSeg = fields.Select(k => $"{runtime.DelimitIdentifier(entityType, k.Name)} = {runtime.DelimitParameter(entityType,k.Field.Name)}").ToArrayString(", ");
            string whereSeg      = KeyDelimitSegment(entityType, runtime, metadata);

            StringBuilder sqlBuilder = new StringBuilder();

            sqlBuilder.Append("UPDATE ");
            sqlBuilder.Append($"{runtime.DelimitIdentifier(entityType, metadata.TableName)} ");
            sqlBuilder.Append($"SET ");
            sqlBuilder.Append(columnsSetSeg);
            if (!string.IsNullOrWhiteSpace(whereSeg))
            {
                sqlBuilder.Append($" WHERE ");
                sqlBuilder.Append(whereSeg);
            }
            return(sqlBuilder.ToString());
        }
Esempio n. 3
0
 private static string KeyDelimitSegment(Type entityType, DapperRuntime runtime, DapperMetadata metadata)
 {
     return(metadata.Fields.Where(f => f.IsKey).Select(k => $"{runtime.DelimitIdentifier(entityType, k.Name)} =  {runtime.DelimitParameter(entityType, k.Field.Name)}").ToArrayString(" AND "));
 }
Esempio n. 4
0
        /// <summary>
        /// 根据字段名和值生成 SET 子句(不包含 SET 字符)。
        /// </summary>
        /// <param name="fieldsAndValues">要生成 SET 子句的字段名和字段值。</param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public string GenerateSetSegments <TEntity>(IEnumerable <KeyValuePair <String, Object> > fieldsAndValues, DynamicParameters parameters)
        {
            Guard.ArgumentNotNull(fieldsAndValues, nameof(fieldsAndValues));

            List <String> setString = new List <String>();

            foreach (var kp in fieldsAndValues)
            {
                string parameterName = $"p{(parameters.ParameterNames.Count() + 1)}";
                parameters.Add($"{parameterName}", kp.Value);
                setString.Add($"{_runtime.DelimitIdentifier(typeof(TEntity), MappingStrategyParser.Parse(kp.Key))} = {_runtime.DelimitParameter(typeof(TEntity), parameterName)}");
            }
            return(setString.ToArrayString(", "));
        }