public static IResultSummary DeleteNode <T>(this IStatementRunner ext, T entity, Expression <Func <T, object> > propMatch = null) where T : class { ext = ext ?? throw new ArgumentNullException(nameof(ext)); entity = entity ?? throw new ArgumentNullException(nameof(entity)); IEnumerable <string> keyOn = propMatch?.ToPropertyNameCollection() ?? entity.GetType().GetProperties().Select(p => p.Name); if (keyOn.Count() > 0) { foreach (string name in keyOn) { PropertyInfo pinfo = typeof(T).GetProperty(name); if (ObjectExtensions.Configuration.Get(pinfo, entity) == pinfo.PropertyType.GetDefault()) { throw new ArgumentException($"Every selection matching property must be set. '{name}' has its default type value."); } } } Symbol s = new Symbol(); Node n = new Node(s, typeof(T), entity.SelectProperties(keyOn)); n.Parametrize(prefix: "value."); return(ext.Execute( $"MATCH {n.BuildForQuery()} " + $"DETACH DELETE {s}", new { value = entity })); }
public override void DeleteRels(IStatementRunner runner, IEnumerable <IOgmConnection> entities) { runner = runner ?? throw new ArgumentNullException(nameof(runner)); entities = entities ?? throw new ArgumentNullException(nameof(entities)); entities = entities.Where(p => p?.EntityId != null); if (entities.Count() == 0) { return; } IGraphManagedStatementRunner mgr = (runner as IGraphManagedStatementRunner) ?? throw new ArgumentException("The statement must be decorated.", nameof(runner)); using (ManagerAccess.Manager.ScopeOMnG()) { Symbol row = new Symbol(); Symbol m = new Symbol(); StringBuilder sb = new StringBuilder(); sb.Append($"UNWIND $batch AS {row} "); sb.Append($"MATCH ()-[{m} {{{nameof(IOgmEntity.EntityId)}:{row}}}]->()"); sb.Append($"DELETE {m}"); runner.Execute(sb.ToString(), new { batch = entities.Select(p => p.EntityId).ToList() }); } }
public static IEnumerable <IResultSummary> Execute(this IStatementRunner ext, string query, params object[] param) { ext = ext ?? throw new ArgumentNullException(nameof(ext)); query = query ?? throw new ArgumentNullException(nameof(query)); if (param == null || param.Length == 0) { yield break; } foreach (object item in param) { yield return(ext.Execute(query, item)); } }
public static IEnumerable <IResultSummary> Execute(this IStatementRunner ext, Func <qu.IQueryBuilder, string> query, params object[] param) { return(ext.Execute(query(new qu.QueryBuilder()), param)); }
public static IResultSummary Execute(this IStatementRunner ext, Func <qu.IQueryBuilder, string> query, object param = null) { return(ext.Execute(query(new qu.QueryBuilder()), param)); }
public override IEnumerable <IOgmConnection> MergeConnections(IStatementRunner runner, IEnumerable <Tuple <IOgmConnection, IEnumerable <string> > > entities) { runner = runner ?? throw new ArgumentNullException(nameof(runner)); entities = entities ?? throw new ArgumentNullException(nameof(entities)); entities = entities.Where(p => p?.Item1 != null); if (entities.Count() == 0) { return(null); } IGraphManagedStatementRunner mgr = (runner as IGraphManagedStatementRunner) ?? throw new ArgumentException("The statement must be decorated.", nameof(runner)); using (ManagerAccess.Manager.ScopeOMnG()) { long version = DateTimeOffset.Now.ToUnixTimeMilliseconds(); List <IGrouping <string, Tuple <int, ConnectionEntity> > > sets = entities .Select((p, i) => new Tuple <int, ConnectionEntity>(i, new ConnectionEntity(p.Item1, p.Item1.Source.EntityId.Value, p.Item1.Destination.EntityId.Value, version, excludePorperties: p.Item2))) .GroupBy(p => p.Item2.Label).ToList(); Symbol row = new Symbol(); Symbol s = new Symbol(); Symbol d = new Symbol(); Symbol r = new Symbol(); List <Tuple <int, IOgmEntity> > results = new List <Tuple <int, IOgmEntity> >(); foreach (var set in sets) { List <Tuple <int, ConnectionEntity> > items = set.ToList(); StringBuilder sb = new StringBuilder(); sb.Append($"UNWIND $batch AS {row} "); sb.Append($"MATCH ({s} {{{nameof(IOgmEntity.EntityId)}:{row}.{nameof(RelEntity.SourceId)}}}) "); sb.Append($"MATCH ({d} {{{nameof(IOgmEntity.EntityId)}:{row}.{nameof(RelEntity.DestinationId)}}}) "); sb.Append($"MERGE ({s})-"); sb.Append($"[{r}:`{set.Key}` {{" + $"{nameof(OgmConnection.SourcePropertyName)}:{row}.{nameof(RelEntity.Properties)}.{nameof(OgmConnection.SourcePropertyName)}," + $"{nameof(OgmConnection.DestinationPropertyName)}:{row}.{nameof(RelEntity.Properties)}.{nameof(OgmConnection.DestinationPropertyName)}," + $"{nameof(OgmConnection.Order)}:{row}.{nameof(RelEntity.Properties)}.{nameof(OgmConnection.Order)}" + $"}}]"); sb.Append($"->({d}) "); sb.Append($"ON CREATE SET {r}+={row}.{nameof(RelEntity.Properties)},{r}.{nameof(IOgmEntity.EntityId)}=id({r}) "); sb.Append($"ON MATCH SET {r}+={row}.{nameof(RelEntity.Properties)},{r}.{nameof(IOgmEntity.EntityId)}=id({r}) "); sb.Append($"RETURN {r}"); object batch = new { batch = set.Select(p => p.Item2.ToPropDictionary()).ToList() }; results.AddRange(runner .ExecuteQuery <IOgmEntity>(sb.ToString(), batch).ToList() .Select((p, i) => new Tuple <int, IOgmEntity>(items[i].Item1, p))); sb.Clear(); sb.Append($"UNWIND $batch AS {row} "); sb.Append($"MATCH ({s} {{{nameof(IOgmEntity.EntityId)}:{row}.{nameof(RelEntity.SourceId)}}}) "); sb.Append($"MATCH ({d} {{{nameof(IOgmEntity.EntityId)}:{row}.{nameof(RelEntity.DestinationId)}}}) "); sb.Append($"MATCH ({s})-"); sb.Append($"[{r}:`{set.Key}` {{" + $"{nameof(OgmConnection.SourcePropertyName)}:{row}.{nameof(RelEntity.Properties)}.{nameof(OgmConnection.SourcePropertyName)}," + $"{nameof(OgmConnection.DestinationPropertyName)}:{row}.{nameof(RelEntity.Properties)}.{nameof(OgmConnection.DestinationPropertyName)}," + $"{nameof(OgmConnection.Order)}:{row}.{nameof(RelEntity.Properties)}.{nameof(OgmConnection.Order)}" + $"}}]"); sb.Append($"->({d}) "); sb.Append($"WHERE EXISTS({r}.{nameof(OgmConnection.Version)}) AND {r}.{nameof(OgmConnection.Version)}<>{row}.{nameof(RelEntity.Properties)}.{nameof(OgmConnection.Version)} "); sb.Append($"DELETE {r}"); runner.Execute(sb.ToString(), batch); } return(results.OrderBy(p => p.Item1).Select(p => p.Item2 as IOgmConnection).ToList()); } }