Skip to content

Commit

Permalink
Adds Model CRUD operations wrappers to BigQueryDataset and BigQueryMo…
Browse files Browse the repository at this point in the history
…del.
  • Loading branch information
amanda-tarafa committed Feb 7, 2020
1 parent 38e755a commit 06e0d6e
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,38 @@ public BigQueryTable GetOrCreateTable(string tableId, TableSchema schema, GetTab
/// <returns>A <see cref="TableReference"/> representing the requested table.</returns>
public TableReference GetTableReference(string tableId) => _client.GetTableReference(Reference.ProjectId, Reference.DatasetId, tableId);

/// <summary>
/// Lists the models within this dataset.
/// This method just creates a <see cref="DatasetReference"/> and delegates to <see cref="BigQueryClient.ListModels(DatasetReference, ListModelsOptions)"/>.
/// </summary>
/// <para>
/// No network requests are made until the returned sequence is enumerated.
/// This means that any exception due to an invalid request will be deferred until that time. Callers should be prepared
/// for exceptions to be thrown while enumerating the results. In addition to failures due to invalid requests, network
/// or service failures can cause exceptions even after the first results have been returned.
/// </para>
/// <param name="options">The options for the operation. May be null, in which case defaults will be supplied.</param>
/// <returns>A sequence of models within this dataset.</returns>
public PagedEnumerable<ListModelsResponse, BigQueryModel> ListModels(ListModelsOptions options = null) =>
_client.ListModels(Reference, options);

/// <summary>
/// Retrieves a model within this dataset.
/// This method just creates a <see cref="ModelReference"/> and delegates to <see cref="BigQueryClient.GetModel(ModelReference, GetModelOptions)"/>.
/// </summary>
/// <param name="modelId">The model ID. Must not be null.</param>
/// <param name="options">The options for the operation. May be null, in which case defaults will be supplied.</param>
/// <returns>The requested model.</returns>
public BigQueryModel GetModel(string modelId, GetModelOptions options = null) =>
_client.GetModel(GetModelReference(modelId), options);

/// <summary>
/// Creates a <see cref="ModelReference"/> for a model within this dataset.
/// </summary>
/// <param name="modelId">The model ID. Must not be null.</param>
/// <returns>A <see cref="ModelReference"/> representing the requested model.</returns>
public ModelReference GetModelReference(string modelId) => _client.GetModelReference(Reference.ProjectId, Reference.DatasetId, modelId);

/// <summary>
/// Deletes this dataset.
/// This method just creates a <see cref="DatasetReference"/> and delegates to <see cref="BigQueryClient.DeleteDataset(DatasetReference, DeleteDatasetOptions)"/>.
Expand Down Expand Up @@ -394,6 +426,32 @@ public Task<BigQueryTable> GetTableAsync(string tableId, GetTableOptions options
public Task<BigQueryTable> GetOrCreateTableAsync(string tableId, TableSchema schema, GetTableOptions getOptions = null, CreateTableOptions createOptions = null, CancellationToken cancellationToken = default) =>
_client.GetOrCreateTableAsync(GetTableReference(tableId), schema, getOptions, createOptions, cancellationToken);

/// <summary>
/// Asynchronously lists the models within this dataset.
/// This method just creates a <see cref="DatasetReference"/> and delegates to <see cref="BigQueryClient.ListModelsAsync(DatasetReference, ListModelsOptions)"/>.
/// </summary>
/// <para>
/// No network requests are made until the returned sequence is enumerated.
/// This means that any exception due to an invalid request will be deferred until that time. Callers should be prepared
/// for exceptions to be thrown while enumerating the results. In addition to failures due to invalid requests, network
/// or service failures can cause exceptions even after the first results have been returned.
/// </para>
/// <param name="options">The options for the operation. May be null, in which case defaults will be supplied.</param>
/// <returns>An asynchronous sequence of models within this dataset.</returns>
public PagedAsyncEnumerable<ListModelsResponse, BigQueryModel> ListModelsAsync(ListModelsOptions options = null) =>
_client.ListModelsAsync(Reference, options);

/// <summary>
/// Asynchronously retrieves a model within this dataset.
/// This method just creates a <see cref="ModelReference"/> and delegates to <see cref="BigQueryClient.GetModel(ModelReference, GetModelOptions)"/>.
/// </summary>
/// <param name="modelId">The model ID. Must not be null.</param>
/// <param name="options">The options for the operation. May be null, in which case defaults will be supplied.</param>
/// <returns>A task representing the asynchronous operation. When complete, the result is
/// the requested table.</returns>
public Task<BigQueryModel> GetModelAsync(string modelId, GetModelOptions options = null) =>
_client.GetModelAsync(GetModelReference(modelId), options);

/// <summary>
/// Deletes this dataset.
/// This method just creates a <see cref="DatasetReference"/> and delegates to <see cref="BigQueryClient.DeleteDatasetAsync(DatasetReference, DeleteDatasetOptions, CancellationToken)"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

using Google.Api.Gax;
using Google.Apis.Bigquery.v2.Data;
using System.Threading;
using System.Threading.Tasks;

namespace Google.Cloud.BigQuery.V2
{
Expand Down Expand Up @@ -61,5 +63,64 @@ public BigQueryModel(BigQueryClient client, Model resource)
_client = GaxPreconditions.CheckNotNull(client, nameof(client));
Resource = GaxPreconditions.CheckNotNull(resource, nameof(resource));
}

/// <summary>
/// Patches this model with fields in the specified resource.
/// </summary>
/// <remarks>
/// This method delegates to <see cref="BigQueryClient.PatchModel(ModelReference, Model, PatchModelOptions)"/>.
/// </remarks>
/// <param name="resource">The resource to patch with. Must not be null.</param>
/// <param name="matchETag">If true, the etag from <see cref="Resource"/> is propagated into <paramref name="resource"/> for
/// optimistic concurrency. Otherwise, <paramref name="resource"/> is left unchanged.</param>
/// <param name="options">The options for the operation. May be null, in which case defaults will be supplied.</param>
/// <returns>The updated model.</returns>
public BigQueryModel Patch(Model resource, bool matchETag, PatchModelOptions options = null)
{
if (matchETag)
{
resource.ETag = Resource.ETag;
}
return _client.PatchModel(Reference, resource, options);
}

/// <summary>
/// Deletes this model.
/// This method just creates a <see cref="ModelReference"/> and delegates to <see cref="BigQueryClient.DeleteModel(ModelReference, DeleteModelOptions)"/>.
/// </summary>
/// <param name="options">The options for the operation. May be null, in which case defaults will be supplied.</param>
public void Delete(DeleteModelOptions options = null) => _client.DeleteModel(Reference, options);

/// <summary>
/// Asynchronously patches this model with fields in the specified resource.
/// </summary>
/// <remarks>
/// This method delegates to <see cref="BigQueryClient.PatchModelAsync(ModelReference, Model, PatchModelOptions, CancellationToken)"/>.
/// </remarks>
/// <param name="resource">The resource to patch with. Must not be null.</param>
/// <param name="matchETag">If true, the etag from <see cref="Resource"/> is propagated into <paramref name="resource"/> for
/// optimistic concurrency. Otherwise, <paramref name="resource"/> is left unchanged.</param>
/// <param name="options">The options for the operation. May be null, in which case defaults will be supplied.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task representing the asynchronous operation. When complete, the result is
/// the updated model.</returns>
public Task<BigQueryModel> PatchAsync(Model resource, bool matchETag, PatchModelOptions options = null, CancellationToken cancellationToken = default)
{
if (matchETag)
{
resource.ETag = Resource.ETag;
}
return _client.PatchModelAsync(Reference, resource, options, cancellationToken);
}

/// <summary>
/// Asynchronously deletes this model.
/// This method just creates a <see cref="ModelReference"/> and delegates to <see cref="BigQueryClient.DeleteModelAsync(ModelReference, DeleteModelOptions, CancellationToken)"/>.
/// </summary>
/// <param name="options">The options for the operation. May be null, in which case defaults will be supplied.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public Task DeleteAsync(DeleteModelOptions options = null, CancellationToken cancellationToken = default) =>
_client.DeleteModelAsync(Reference, options, cancellationToken);
}
}

0 comments on commit 06e0d6e

Please sign in to comment.