internal static string GetSql(this ISqlTemplate template, string parameterPrefix) { var sql = parameterRegex.Replace(template.TransformText(), (match) => parameterPrefix + match.Value.Substring(2, match.Value.Length - 3)); return(sql); }
protected void Execute(ISqlTemplate sqlTemplate, object model) { using (var command = CreateCommand(sqlTemplate, model)) { command.ExecuteNonQuery(); } }
public TemplatedCommand(DataService dataService, ISqlTemplate template) : base(dataService, true) { if (dataService == null) throw new ArgumentNullException("dataService"); if (template == null) throw new ArgumentNullException("template"); this.Template = template; }
private DbCommand CreateCommand(ISqlTemplate sqlTemplate, object model) { var command = Connection.CreateCommand(); sqlTemplate.Process(command, model); return(command); }
public TemplatedCommand(DataService dataService, ISqlTemplate template) : base(dataService, true) { if (dataService == null) { throw new ArgumentNullException(nameof(dataService)); } if (template == null) { throw new ArgumentNullException(nameof(template)); } this.Template = template; }
protected IEnumerable <T> ExecuteEnumerable <T>(ISqlTemplate sqlTemplate, object model, int methodIndex, Func <DataReader, T> parse) { using (var command = CreateCommand(sqlTemplate, model)) { using (var dbDataReader = command.ExecuteReader(CommandBehavior.Default)) { var dataReader = new DataReader(dbDataReader, Map[methodIndex]); while (dbDataReader.Read()) { yield return(parse(dataReader)); } } } }
protected T Execute <T>(ISqlTemplate sqlTemplate, object model) { var result = default(T); using (var command = CreateCommand(sqlTemplate, model)) { using (var dbDataReader = command.ExecuteReader(CommandBehavior.Default)) { if (dbDataReader.Read()) { result = (T)Get(typeof(T)).Invoke(dbDataReader, 0); } } } return(result); }
public override string GetFromClauseContent(DbCommand selectCommand, ref int paramIndex, int indentation) { ISqlTemplate template = ((ITemplatedQueryLite)this.QueryLite).Template; string parameterPrefix = this.QueryLite.DataService.EntityLiteProvider.ParameterPrefix; template.AddParametersToCommand(selectCommand, this.QueryLite.DataService); if (string.IsNullOrEmpty(sql)) { var sb = new StringBuilder(); sb.NewIndentedLine(++indentation); var indentedLine = sb.ToString(); sql = template.GetSql(parameterPrefix).Replace("\r\n", indentedLine); sb = new StringBuilder(); sb.Append('(').Append('\n').Append(sql).NewIndentedLine(--indentation).Append(") TQ"); sql = sb.ToString(); } return(sql); }
internal static void AddParametersToCommand(this ISqlTemplate template, DbCommand command, DataService dataService) { if (template == null) { throw new ArgumentNullException(nameof(template)); } if (command == null) { throw new ArgumentNullException(nameof(command)); } Type templateType = template.GetType(); var getters = templateType.GetPropertyGetters(); var parameterPrefix = dataService.EntityLiteProvider.ParameterPrefix; foreach (var pi in templateType.GetProperties()) { var attrs = pi.GetCustomAttributes(typeof(DbParameterAttribute), false); if (attrs.Length > 0) { var parameterAttr = (DbParameterAttribute)attrs[0]; IDbDataParameter parameter = command.CreateParameter(); parameter.ParameterName = parameterPrefix + pi.Name; parameter.SourceColumn = pi.Name; if (parameterAttr.ProviderType == int.MaxValue) { parameter.DbType = parameterAttr.DbType; } else { dataService.EntityLiteProvider.SetProviderTypeToParameter(parameter, parameterAttr.ProviderType); } parameter.Direction = parameterAttr.Direction; if (parameterAttr.Size != 0) { parameter.Size = parameterAttr.Size; } if (parameterAttr.Precision != 0) { parameter.Precision = parameterAttr.Precision; } if (parameterAttr.Scale != 0) { parameter.Scale = parameterAttr.Scale; } object propValue = getters[pi.Name](template); parameter.Value = propValue == null ? DBNull.Value : propValue; command.Parameters.Add(parameter); } attrs = pi.GetCustomAttributes(typeof(DbParameterSerieAttribute), false); if (attrs.Length > 0) { var parameterAttr = (DbParameterSerieAttribute)attrs[0]; object propValue = getters[pi.Name](template); if (propValue != null) { var items = propValue as IEnumerable; if (items == null) { throw new ArgumentException(pi.Name + " property is not enumerable. Properties decorated with DbParameterSerieAttribute must be enumerable"); } int i = 0; foreach (object item in items) { IDbDataParameter parameter = command.CreateParameter(); parameter.ParameterName = parameterPrefix + pi.Name + i.ToString(CultureInfo.InvariantCulture); parameter.SourceColumn = pi.Name + i.ToString(CultureInfo.InvariantCulture); if (parameterAttr.ProviderType == int.MaxValue) { parameter.DbType = parameterAttr.DbType; } else { dataService.EntityLiteProvider.SetProviderTypeToParameter(parameter, parameterAttr.ProviderType); } parameter.Direction = parameterAttr.Direction; if (parameterAttr.Size != 0) { parameter.Size = parameterAttr.Size; } if (parameterAttr.Precision != 0) { parameter.Precision = parameterAttr.Precision; } if (parameterAttr.Scale != 0) { parameter.Scale = parameterAttr.Scale; } parameter.Value = item == null ? DBNull.Value : item; command.Parameters.Add(parameter); i++; } } } } }
protected T Execute <T>(ISqlTemplate sqlTemplate, object model, int methodIndex, Func <DataReader, T> parse) { return(ExecuteEnumerable(sqlTemplate, model, methodIndex, parse).FirstOrDefault()); }
public TemplatedQueryLite(DataService dataAccess, ISqlTemplate template) : this() { this.DataService = dataAccess; this.Template = template; }