Skip to content

Commit

Permalink
docs: add dotnet samples
Browse files Browse the repository at this point in the history
  • Loading branch information
olavloite committed May 27, 2024
1 parent 9065008 commit 42a94f2
Show file tree
Hide file tree
Showing 140 changed files with 3,183 additions and 5 deletions.
34 changes: 34 additions & 0 deletions samples/snippets/dotnet-snippets/AddColumn.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// [START spanner_add_column]
using Npgsql;

namespace dotnet_snippets;

static class AddColumnSample
{
internal static void AddColumn(string host, int port, string database)
{
var connectionString = $"Host={host};Port={port};Database={database};SSL Mode=Disable;Pooling=False";
using var connection = new NpgsqlConnection(connectionString);
connection.Open();

using var cmd = connection.CreateCommand();
cmd.CommandText = "alter table albums add column marketing_budget bigint";
cmd.ExecuteNonQuery();
Console.WriteLine("Added marketing_budget column");
}
}
// [END spanner_add_column]
2 changes: 1 addition & 1 deletion samples/snippets/dotnet-snippets/CreateConnectionSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static class CreateConnectionSample
{
internal static void CreateConnection(string host, int port, string database)
{
var connectionString = $"Host={host};Port={port};Database={database};SSL Mode=Disable";
var connectionString = $"Host={host};Port={port};Database={database};SSL Mode=Disable;Pooling=False";
using var connection = new NpgsqlConnection(connectionString);
connection.Open();

Expand Down
2 changes: 1 addition & 1 deletion samples/snippets/dotnet-snippets/CreateTablesSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static class CreateTablesSample
{
internal static void CreateTables(string host, int port, string database)
{
var connectionString = $"Host={host};Port={port};Database={database};SSL Mode=Disable";
var connectionString = $"Host={host};Port={port};Database={database};SSL Mode=Disable;Pooling=False";
using var connection = new NpgsqlConnection(connectionString);
connection.Open();

Expand Down
45 changes: 45 additions & 0 deletions samples/snippets/dotnet-snippets/DataBoost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// [START spanner_data_boost]
using Npgsql;

namespace dotnet_snippets;

static class DataBoostSample
{
internal static void DataBoost(string host, int port, string database)
{
var connectionString = $"Host={host};Port={port};Database={database};SSL Mode=Disable;Pooling=False";
using var connection = new NpgsqlConnection(connectionString);
connection.Open();

using var cmd = connection.CreateCommand();
// This enables Data Boost for all partitioned queries on this connection.
cmd.CommandText = "set spanner.data_boost_enabled=true";
cmd.ExecuteNonQuery();


// Run a partitioned query. This query will use Data Boost.
cmd.CommandText = "run partitioned query "
+ "select singer_id, first_name, last_name "
+ "from singers";
using var reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine($"{reader["singer_id"]} {reader["first_name"]} {reader["last_name"]}");
}
}
}
// [END spanner_data_boost]
52 changes: 52 additions & 0 deletions samples/snippets/dotnet-snippets/DdlBatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// [START spanner_ddl_batch]
using Npgsql;

namespace dotnet_snippets;

static class DdlBatchSample
{
internal static void DdlBatch(string host, int port, string database)
{
var connectionString = $"Host={host};Port={port};Database={database};SSL Mode=Disable;Pooling=False";
using var connection = new NpgsqlConnection(connectionString);
connection.Open();

// Create two new tables in one batch.
var batch = connection.CreateBatch();
batch.BatchCommands.Add(new NpgsqlBatchCommand(
"CREATE TABLE venues ("
+ " venue_id bigint not null primary key,"
+ " name varchar(1024),"
+ " description jsonb"
+ ")"));
batch.BatchCommands.Add(new NpgsqlBatchCommand(
"CREATE TABLE concerts ("
+ " concert_id bigint not null primary key ,"
+ " venue_id bigint not null,"
+ " singer_id bigint not null,"
+ " start_time timestamptz,"
+ " end_time timestamptz,"
+ " constraint fk_concerts_venues foreign key"
+ " (venue_id) references venues (venue_id),"
+ " constraint fk_concerts_singers foreign key"
+ " (singer_id) references singers (singer_id)"
+ ")"));
batch.ExecuteNonQuery();
Console.WriteLine("Added venues and concerts tables");
}
}
// [END spanner_ddl_batch]
40 changes: 40 additions & 0 deletions samples/snippets/dotnet-snippets/PartitionedDml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// [START spanner_partitioned_dml]
using Npgsql;

namespace dotnet_snippets;

static class PartitionedDmlSample
{
internal static void PartitionedDml(string host, int port, string database)
{
var connectionString = $"Host={host};Port={port};Database={database};SSL Mode=Disable;Pooling=False";
using var connection = new NpgsqlConnection(connectionString);
connection.Open();

// Enable Partitioned DML on this connection.
using var cmd = connection.CreateCommand();
cmd.CommandText = "set spanner.autocommit_dml_mode='partitioned_non_atomic'";
cmd.ExecuteNonQuery();

// Back-fill a default value for the MarketingBudget column.
cmd.CommandText = "update albums set marketing_budget=0 where marketing_budget is null";
var lowerBoundUpdateCount = cmd.ExecuteNonQuery();

Console.WriteLine($"Updated at least {lowerBoundUpdateCount} albums");
}
}
// [END spanner_partitioned_dml]
36 changes: 36 additions & 0 deletions samples/snippets/dotnet-snippets/QueryDataSample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// [START spanner_query_data]
using Npgsql;

namespace dotnet_snippets;

static class QueryDataSample
{
internal static void QueryData(string host, int port, string database)
{
var connectionString = $"Host={host};Port={port};Database={database};SSL Mode=Disable;Pooling=False";
using var connection = new NpgsqlConnection(connectionString);
connection.Open();

using var cmd = new NpgsqlCommand("SELECT singer_id, album_id, album_title FROM albums", connection);
using var reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine($"{reader.GetInt64(0)} {reader.GetInt64(1)} {reader.GetString(2)}");
}
}
}
// [END spanner_query_data]
38 changes: 38 additions & 0 deletions samples/snippets/dotnet-snippets/QueryDataWithNewColumnSample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// [START spanner_query_data_with_new_column]
using Npgsql;

namespace dotnet_snippets;

static class QueryDataWithNewColumnSample
{
internal static void QueryWithNewColumnData(string host, int port, string database)
{
var connectionString = $"Host={host};Port={port};Database={database};SSL Mode=Disable;Pooling=False";
using var connection = new NpgsqlConnection(connectionString);
connection.Open();

using var cmd = new NpgsqlCommand("SELECT singer_id, album_id, marketing_budget "
+ "FROM albums "
+ "ORDER BY singer_id, album_id", connection);
using var reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine($"{reader["singer_id"]} {reader["album_id"]} {reader["marketing_budget"]}");
}
}
}
// [END spanner_query_data_with_new_column]
39 changes: 39 additions & 0 deletions samples/snippets/dotnet-snippets/QueryDataWithParameterSample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// [START spanner_query_with_parameter]
using Npgsql;

namespace dotnet_snippets;

static class QueryDataWithParameterSample
{
internal static void QueryDataWithParameter(string host, int port, string database)
{
var connectionString = $"Host={host};Port={port};Database={database};SSL Mode=Disable;Pooling=False";
using var connection = new NpgsqlConnection(connectionString);
connection.Open();

using var cmd = new NpgsqlCommand("SELECT singer_id, first_name, last_name "
+ "FROM singers "
+ "WHERE last_name = $1", connection);
cmd.Parameters.Add(new NpgsqlParameter { Value = "Garcia" });
using var reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine($"{reader["singer_id"]} {reader["first_name"]} {reader["last_name"]}");
}
}
}
// [END spanner_query_with_parameter]
46 changes: 46 additions & 0 deletions samples/snippets/dotnet-snippets/QueryWithTimeout.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// [START spanner_statement_timeout]
using Npgsql;

namespace dotnet_snippets;

static class QueryWithTimeoutSample
{
internal static void QueryWithTimeout(string host, int port, string database)
{
var connectionString = $"Host={host};Port={port};Database={database};SSL Mode=Disable;Pooling=False";
using var connection = new NpgsqlConnection(connectionString);
connection.Open();

using var cmd = new NpgsqlCommand("SELECT singer_id, album_id, album_title "
+ "FROM albums "
+ "WHERE album_title in ("
+ " SELECT first_name "
+ " FROM singers "
+ " WHERE last_name LIKE '%a%'"
+ " OR last_name LIKE '%m%'"
+ ")", connection);
// npgsql has built-in support for setting query timeouts.
// This sets the query timeout for this statement to 5 seconds.
cmd.CommandTimeout = 5;
using var reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine($"{reader["singer_id"]} {reader["album_id"]} {reader["album_title"]}");
}
}
}
// [END spanner_statement_timeout]
Loading

0 comments on commit 42a94f2

Please sign in to comment.