private void UpsertToDatastore <T>(IEnumerable <T> entities) where T : new() { var datastore = new Google.Apis.Datastore.v1beta2.DatastoreService(Authenticator.GetInitializer()); var transaction = datastore.Datasets.BeginTransaction(new BeginTransactionRequest(), Authenticator.GetProjectId()).Execute(); var datastoreEntities = GetSerializer <T>().SerializeAndAutoKey(entities, Authenticator, Configuration.DoubleCheckGeneratedIds); datastore.Datasets.Commit(new CommitRequest { Mutation = new Mutation { Upsert = datastoreEntities }, Mode = "TRANSACTIONAL", Transaction = transaction.Transaction }, Authenticator.GetProjectId()).Execute(); }
private void Delete(IEnumerable <string> keys, string kind) { var datastore = new Google.Apis.Datastore.v1beta2.DatastoreService(Authenticator.GetInitializer()); var transaction = datastore.Datasets.BeginTransaction(new BeginTransactionRequest(), Authenticator.GetProjectId()).Execute(); datastore.Datasets.Commit(new CommitRequest { Mutation = new Mutation { Delete = keys.Select(x => new Key { Path = new [] { new KeyPathElement { Kind = kind, Name = x } } }).ToList() }, Mode = "TRANSACTIONAL", Transaction = transaction.Transaction }, Authenticator.GetProjectId()).Execute(); }
private bool IsKeyUsed(CloudAuthenticator authenticator, string key) { // TODO consider using AllocateIds() var result = new Google.Apis.Datastore.v1beta2.DatastoreService(authenticator.GetInitializer()).Datasets.Lookup(new LookupRequest { Keys = new List <Key> { new Key { Path = new List <KeyPathElement> { new KeyPathElement { Name = key, Kind = typeof(T).Name } } } } }, authenticator.GetProjectId()).Execute(); return(result.Found.Any()); }
public override object Execute(Expression expression) { var state = Translate(expression); // Perform local indexing if needed if (Configuration.GenerateIndexYAMLFile) { BuildIndex(state); } // Build a query var datastore = new Google.Apis.Datastore.v1beta2.DatastoreService(Authenticator.GetInitializer()); var gql = new Google.Apis.Datastore.v1beta2.Data.GqlQuery { QueryString = state.QueryBuilder.ToString(), NameArgs = state.Parameters.Select(x => new GqlQueryArg { Name = x.ParameterName, Value = ReadQuery_ConvertTypeToValueType(x.ParameterName, x.Value, x.TypeCode) }).ToList(), AllowLiteral = false // enforce parameterized queries }; // Grab results var result = datastore.Datasets.RunQuery(new RunQueryRequest { GqlQuery = gql }, Authenticator.GetProjectId()).Execute(); // Project if necessary / Select() method if (state.Projector != null) { var elementType = TypeSystem.GetElementType(expression.Type); var projector = state.Projector.Compile(); return(Activator.CreateInstance( typeof(ProjectionReader <>).MakeGenericType(elementType), BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { result.Batch.EntityResults.Select(x => x.Entity), projector }, null )); } // First()/Single() method if (state.QueryState.HasFlag(QueryState.IsFirst) || state.QueryState.HasFlag(QueryState.IsSingle)) { if (result.Batch.EntityResults.Count == 0 && !state.QueryState.HasFlag(QueryState.AllowFirstSingleOrDefault)) { throw new InvalidOperationException("Sequence contains no elements"); } if (result.Batch.EntityResults.Count > 1 && state.QueryState.HasFlag(QueryState.IsSingle)) { throw new InvalidOperationException("Sequence contains more than one element"); } if (result.Batch.EntityResults.Any()) { return(_serializer.DeserializeEntity(result.Batch.EntityResults[0].Entity)); } return(null); } // Any() method if (state.QueryState.HasFlag(QueryState.IsAny)) { return(result.Batch.EntityResults.Any()); } // Regular Where() query return(result.Batch.EntityResults.Select(entityResult => _serializer.DeserializeEntity(entityResult.Entity)).ToList()); }