private SqlPreCommandSimple DeclarePrimaryKeyVariable <T>(T entity, Expression <Func <T, bool> > where) where T : Entity { var query = Database.Query <T>().Where(where); var uniqueFilters = Schema.Current.AttachToUniqueFilter?.GetInvocationListTyped().Select(f => f.Invoke(entity)).NotNull().ToList(); if (uniqueFilters != null && uniqueFilters.Any()) { uniqueFilters.ForEach(f => query = query.Where(e => f.Evaluate(e))); } var queryCommand = DbQueryProvider.Single.GetMainSqlCommand(query.Select(a => a.Id).Expression); string variableName = this.Name.Name + "Id_" + (parameterIndex++); if (!Schema.Current.Settings.IsPostgres) { variableName = SqlParameterBuilder.GetParameterName(variableName); } entity.SetId(new Entities.PrimaryKey(entity.id !.Value.Object, variableName)); string queryCommandString = queryCommand.PlainSql().Lines().ToString(" "); var result = Schema.Current.Settings.IsPostgres ? new SqlPreCommandSimple(@$ "{variableName} {Connector.Current.SqlBuilder.GetColumnType(this.PrimaryKey)} = ({queryCommandString});") : new SqlPreCommandSimple($"DECLARE {variableName} {Connector.Current.SqlBuilder.GetColumnType(this.PrimaryKey)}; SET {variableName} = COALESCE(({queryCommandString}), 1 / 0);"); return(result); }
public void SqlBagTest() { var whereBuilder = new SqlWhereClauseBuilder(); this._operators.ForEach(o => { whereBuilder.AddParameterValue(o.Item1, o.Item2, o.Item3); }); var parameters = whereBuilder.GetParameterAndValues(); var parameterBuilder = new SqlParameterBuilder(); foreach (var p in parameters) { parameterBuilder.AddParameterValue(p.Key, p.Value); } var parames = parameterBuilder.GetParameters(); var result = new StringBuilder(); foreach (var p in parames) { result.AppendFormat("{0}:{1};", p.ParameterName, p.Value); } Assert.Fail(result.ToString(), result); }
protected override void AddParametersFor(SpecialOwnerDto input, SqlParameterBuilder builder) => builder.ForParameter(input.Id, ID_COLUMN, SqlDbType.BigInt) .ForParameter(input.CreationDate, CREATION_DATE_COLUMN, SqlDbType.DateTime2) .ForParameter(input.CreatedBy, CREATED_BY_COLUMN, SqlDbType.VarChar) .ForParameter(input.ModificationDate, MODIFICATION_DATE_COLUMN, SqlDbType.DateTime2) .ForParameter(input.ModifiedBy, MODIFIED_BY_COLUMN, SqlDbType.VarChar) .ForParameter(input.Name, NAME_COLUMN, SqlDbType.VarChar) .ForParameter(input.Remark, REMARK_COLUMN, SqlDbType.VarChar);
protected override void AddParametersFor(ProductDto input, SqlParameterBuilder builder) => builder.ForParameter(input.Id, ID_COLUMN, SqlDbType.BigInt) .ForParameter(input.CreationDate, CREATION_DATE_COLUMN, SqlDbType.DateTime2) .ForParameter(input.CreatedBy, CREATED_BY_COLUMN, SqlDbType.VarChar) .ForParameter(input.ModificationDate, MODIFICATION_DATE_COLUMN, SqlDbType.DateTime2) .ForParameter(input.ModifiedBy, MODIFIED_BY_COLUMN, SqlDbType.VarChar) .ForParameter(input.Brand, BRAND_COLUMN, SqlDbType.VarChar) .ForParameter(input.Type, TYPE_COLUMN, SqlDbType.VarChar);
public void BuildSqlParameter_ModelWithoutAttributes_ThrowsException() { var sqlParamBuilder = new SqlParameterBuilder(); var fakeModelWithoutAttributes = new FakeModelWithoutAttributes(); var param_propertyName = "FakeProperty"; var param_propertyValue = fakeModelWithoutAttributes.FakeProperty; Assert.Throws <MissingAttributeException>( () => sqlParamBuilder.BuildSqlParameter <FakeModelWithoutAttributes>( param_propertyName, param_propertyValue)); }
private SqlPreCommand DeclarePrimaryKeyVariable <T>(T entity, Expression <Func <T, bool> > where) where T : Entity { var query = DbQueryProvider.Single.GetMainSqlCommand(Database.Query <T>().Where(where).Select(a => a.Id).Expression); string variableName = SqlParameterBuilder.GetParameterName(this.Name.Name + "Id_" + (parameterIndex++)); entity.SetId(new Entities.PrimaryKey(entity.id.Value.Object, variableName)); string queryString = query.PlainSql().Lines().ToString(" "); var result = new SqlPreCommandSimple($"DECLARE {variableName} {SqlBuilder.GetColumnType(this.PrimaryKey)}; SET {variableName} = COALESCE(({queryString}), 1 / 0)"); return(result); }
public void BuildSqlParameter_ModelWithAttributes_ReturnsExpectedSqlParameter() { var sqlParamBuilder = new SqlParameterBuilder(); var fakeModelWithAttributes = new FakeBlogModel(); var param_propertyName = "FakeProperty"; var param_propertyValue = fakeModelWithAttributes.FakeProperty; var expected = new SqlParameter { ParameterName = "@fakecolumn", SqlDbType = SqlDbType.NChar, Value = param_propertyValue }; var actual = sqlParamBuilder.BuildSqlParameter <FakeBlogModel>(param_propertyName, param_propertyValue); AssertSqlParameterEqual(expected, actual); }
protected override void AddParametersFor(AssetHistoryDto input, SqlParameterBuilder builder) => throw new NotImplementedException();
protected abstract void AddParametersFor(TDto input, SqlParameterBuilder builder);