-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
140 changed files
with
3,183 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
samples/snippets/dotnet-snippets/QueryDataWithNewColumnSample.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
39
samples/snippets/dotnet-snippets/QueryDataWithParameterSample.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
Oops, something went wrong.