public UpdateStatementsBuilder <TModel> SetField <TValue>(Expression <Func <TModel, TValue> > field, TValue value) { var pi = NodeVisitor.VisitPath(field, null); var npgValue = PgTypesConverter.ConvertToPgValue(pi.Expression.Type, value); if (pi.JsonPath.Count > 0) { AddUpdate(pi.Column, src => ExpressionBuilders.JsonSet(src, pi.JsonPath, TypedExpression.FromPgValue(npgValue))); } else { AddUpdate(pi.Column, src => TypedExpression.FromPgValue(npgValue)); } return(this); }
public RawQuery GetRawQuery() { if (IsEmpty) { return(RawQuery.Create("SELECT 0")); } RawQuery rq = new RawQuery(); var columns = _columns.Count == 0 ? AllColumnsWithoutAutoIncrement : _columns; rq.Append("INSERT INTO "); rq.AppendTableName(Table.Name, Table.Schema); rq.Append("("); rq.AppendColumnNames(columns.Select(x => x.columnDescriptor.Name)); if (_idColumn != null) { if (columns.Count > 0) { rq.Append(","); } rq.AppendColumnName(_idColumn.Name); } rq.Append(")"); rq.Append(" VALUES "); var first = true; foreach (var obj in _objects) { if (!first) { rq.Append(","); } rq.Append("("); for (int i = 0; i < columns.Count; i++) { var column = columns[i]; if (i > 0) { rq.Append(","); } if (column.subquery == null) { object val = ModelsRegistry.GetModelValueByColumn(obj, column.columnDescriptor); var npgValue = PgTypesConverter.ConvertToPgValue(column.columnDescriptor.Type, val); rq.Append(npgValue); } else { rq.AppendSurround(column.subquery.GetRawQuery()); } } if (_idColumn != null) { if (columns.Count > 0) { rq.Append(","); } rq.Append(ExpressionBuilders.CurrSeqValueOfTable(_idRefColumn).RawQuery); } rq.Append(")"); first = false; } if (_onInsertConflict == OnInsertConflict.DoNothing) { rq.Append(" ON CONFLICT DO NOTHING "); } if (_onInsertConflict == OnInsertConflict.DoUpdate) { rq.Append(" ON CONFLICT ("); var fields = new FieldListBuilder <TModel>(); _onInsertConflictUpdateFields(fields); first = true; foreach (var column in fields.Fields) { if (!first) { rq.Append(", "); } rq.AppendColumnName(column.Name); first = false; } rq.Append(") DO UPDATE SET "); var updateStatementsBuilder = new UpdateStatementsBuilder <TModel>(); _onInsertConflictUpdate(updateStatementsBuilder); first = true; foreach (var(column, typedExpression) in updateStatementsBuilder.UpdateParts) { if (!first) { rq.Append(", "); } rq.AppendColumnName(column.Name) .Append(" = ") .Append(typedExpression.RawQuery); first = false; } } if (TableModel.PrimaryKey != null) { rq.Append(" RETURNING "); rq.AppendColumnName(TableModel.PrimaryKey.Name); } rq.Append(";"); if (_outputVariable != null) { rq.Append(" SELECT "); rq.Append(ExpressionBuilders.SetConfigText(_outputVariable, ExpressionBuilders.LastVal(), true).RawQuery); } rq.SkipExplicitColumnTableNames(); return(rq); }