public bool VisitExprInsertValues(ExprInsertValues expr, TCtx arg) { var res = this.Visit(expr, "InsertValues", arg, out var argOut) && this.Accept("Items", expr.Items, argOut); this._visitor.EndVisitExpr(expr, arg); return(res); }
public bool VisitExprInsertValues(ExprInsertValues exprInsertValues, IExpr?parent) { this.Builder.Append("VALUES "); for (var i = 0; i < exprInsertValues.Items.Count; i++) { var rowValue = exprInsertValues.Items[i]; if (i > 0) { this.Builder.Append(','); } rowValue.Accept(this, exprInsertValues); } return(true); }
public static ExprInsertValues WithItems(this ExprInsertValues original, IReadOnlyList <ExprInsertValueRow> newItems) => new ExprInsertValues(items: newItems);
public ExprInsert Done() { var checkExistence = this._checkExistenceByColumns != null && this._checkExistenceByColumns.Count > 0; var useDerivedTable = this._targetInsertSelectMapping != null || checkExistence; var mapping = this._dataMapping.AssertFatalNotNull(nameof(this._dataMapping)); int?capacity = this._data.TryToCheckLength(out var c) ? c : (int?)null; if (capacity != null && capacity.Value < 1) { throw new SqExpressException("Input data should not be empty"); } List <ExprValueRow>? recordsS = null; List <ExprInsertValueRow>?recordsI = null; if (useDerivedTable) { recordsS = capacity.HasValue ? new List <ExprValueRow>(capacity.Value) : new List <ExprValueRow>(); } else { recordsI = capacity.HasValue ? new List <ExprInsertValueRow>(capacity.Value) : new List <ExprInsertValueRow>(); } DataMapSetter <TTable, TItem>? dataMapSetter = null; IReadOnlyList <ExprColumnName>?columns = null; foreach (var item in this._data) { dataMapSetter ??= new DataMapSetter <TTable, TItem>(this._target, item); dataMapSetter.NextItem(item, columns?.Count); mapping(dataMapSetter); columns ??= dataMapSetter.Columns; dataMapSetter.EnsureRecordLength(); recordsS?.Add(new ExprValueRow(dataMapSetter.Record.AssertFatalNotNull(nameof(dataMapSetter.Record)))); recordsI?.Add(new ExprInsertValueRow(dataMapSetter.Record.AssertFatalNotNull(nameof(dataMapSetter.Record)))); } if ((recordsS?.Count ?? 0 + recordsI?.Count ?? 0) < 1 || columns == null) { //In case of empty IEnumerable throw new SqExpressException("Input data should not be empty"); } IExprInsertSource insertSource; if (recordsI != null) { insertSource = new ExprInsertValues(recordsI); } else if (recordsS != null && useDerivedTable) { var valuesConstructor = new ExprTableValueConstructor(recordsS); var values = new ExprDerivedTableValues( valuesConstructor, new ExprTableAlias(Alias.Auto.BuildAliasExpression().AssertNotNull("Alias cannot be null")), columns); IReadOnlyList <ColumnValueInsertSelectMap>?additionalMaps = null; if (this._targetInsertSelectMapping != null) { var targetUpdateSetter = new TargetInsertSelectSetter <TTable>(this._target); this._targetInsertSelectMapping.Invoke(targetUpdateSetter); additionalMaps = targetUpdateSetter.Maps; if (additionalMaps.Count < 1) { throw new SqExpressException("Additional insertion cannot be null"); } } var selectValues = new List <IExprSelecting>(columns.Count + (additionalMaps?.Count ?? 0)); foreach (var exprColumnName in values.Columns) { selectValues.Add(exprColumnName); } if (additionalMaps != null) { foreach (var m in additionalMaps) { selectValues.Add(m.Value); } } IExprQuery query; var queryBuilder = SqQueryBuilder.Select(selectValues).From(values); if (checkExistence && this._checkExistenceByColumns != null) { var tbl = this._target.WithAlias(new ExprTableAlias(Alias.Auto.BuildAliasExpression() !)); var existsFilter = !SqQueryBuilder.Exists(SqQueryBuilder .SelectOne() .From(tbl) .Where(this._checkExistenceByColumns .Select(column => column.WithSource(tbl.Alias) == column.WithSource(values.Alias)) .JoinAsAnd())); query = queryBuilder.Where(existsFilter).Done(); } else { query = queryBuilder.Done(); } insertSource = new ExprInsertQuery(query); if (additionalMaps != null) { var extraInsertCols = additionalMaps.SelectToReadOnlyList(m => m.Column); columns = Helpers.Combine(columns, extraInsertCols); } } else { //Actually C# should have detected that this brunch cannot be invoked throw new SqExpressException("Fatal logic error!"); } return(new ExprInsert(this._target.FullName, columns, insertSource)); }
public ExprIdentityInsert Values(ExprInsertValues values) { var exprInsert = new ExprInsert(this._table.FullName, this._columns, values); return(new ExprIdentityInsert(exprInsert, this.IdentityColumns())); }
public ExprInsert Values(ExprInsertValues values) { return(new ExprInsert(this._table.FullName, this._columns, values)); }