-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First draft of PostgreSQL Dialact samples
- Loading branch information
Showing
9 changed files
with
252 additions
and
68 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
spanner/api/Spanner.Samples.Tests/CastDatatypesAsyncPostgreTest.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,45 @@ | ||
// Copyright 2022 Google Inc. | ||
// | ||
// 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. | ||
|
||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
[Collection(nameof(SpannerFixture))] | ||
public class CastDatatypesAsyncPostgreTest | ||
{ | ||
private readonly SpannerFixture _spannerFixture; | ||
|
||
private readonly CastDatatypesAsyncPostgreSample _sample; | ||
|
||
public CastDatatypesAsyncPostgreTest(SpannerFixture spannerFixture) | ||
{ | ||
_spannerFixture = spannerFixture; | ||
_sample = new CastDatatypesAsyncPostgreSample(); | ||
} | ||
|
||
[Fact] | ||
public async Task TestCastDatatypesAsyncPostgre() | ||
{ | ||
// Act. | ||
var result = await _sample.CastDatatypesAsyncPostgre(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.PostgreSqlDatabaseId); | ||
//Assert. | ||
Assert.Contains("String : 1", result); | ||
Assert.Contains("Integer : 2", result); | ||
Assert.Contains("Decimal : 3", result); | ||
Assert.Contains("Bytes : 34", result); | ||
Assert.Contains("Float : 5.00", result); | ||
Assert.Contains("Bool : True", result); | ||
Assert.Contains("Timestamp : 2021-11-03 09:35:01Z", result); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
spanner/api/Spanner.Samples.Tests/ConnectToDatabaseAsyncPostgreTest.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,40 @@ | ||
// Copyright 2022 Google Inc. | ||
// | ||
// 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. | ||
|
||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
[Collection(nameof(SpannerFixture))] | ||
public class ConnectToDatabaseAsyncPostgreTest | ||
{ | ||
private readonly SpannerFixture _spannerFixture; | ||
|
||
private readonly ConnectToDatabaseAsyncPostgreSample _sample; | ||
|
||
public ConnectToDatabaseAsyncPostgreTest(SpannerFixture spannerFixture) | ||
{ | ||
_spannerFixture = spannerFixture; | ||
_sample = new ConnectToDatabaseAsyncPostgreSample(); | ||
} | ||
|
||
[Fact] | ||
public async Task TestConnectToDatabaseAsyncPostgre() | ||
{ | ||
// Act. | ||
var result = await _sample.ConnectToDatabaseAsyncPostgre(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.PostgreSqlDatabaseId); | ||
|
||
//Assert. | ||
Assert.Equal($"Hello from Spanner PostgreSQL!", result); | ||
} | ||
} |
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,55 @@ | ||
// Copyright 2022 Google Inc. | ||
// | ||
// 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_postgresql_cast_data_type] | ||
using Google.Cloud.Spanner.Data; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
public class CastDatatypesAsyncPostgreSample | ||
{ | ||
public async Task<string> CastDatatypesAsyncPostgre(string projectId, string instanceId, string databaseId) | ||
{ | ||
string connectionString = $"Data Source=projects/{projectId}/instances/{instanceId}/databases/{databaseId}"; | ||
|
||
using var connection = new SpannerConnection(connectionString); | ||
await connection.OpenAsync(); | ||
|
||
// The `::` cast operator can be used to cast from one data type to another. | ||
var commandText = "SELECT 1::varchar as str, '2'::int as int, 3::decimal as dec, '4'::bytea as bytes, " | ||
+ "5::float as float, 'true'::bool as bool, '2021-11-03T09:35:01UTC'::timestamptz as timestamp"; | ||
var command = connection.CreateSelectCommand(commandText); | ||
|
||
using var reader = await command.ExecuteReaderAsync(); | ||
var resultBuilder = new StringBuilder(); | ||
while (await reader.ReadAsync()) | ||
{ | ||
resultBuilder.AppendLine($"String : {reader.GetFieldValue<string>("str")}"); | ||
resultBuilder.AppendLine($"Integer : {reader.GetFieldValue<int>("int")}"); | ||
resultBuilder.AppendLine($"Decimal : {reader.GetFieldValue<decimal>("dec")}"); | ||
resultBuilder.AppendLine($"Bytes : {BitConverter.ToString(reader.GetFieldValue<byte[]>("bytes"))}"); | ||
resultBuilder.AppendLine($"Float : {reader.GetFieldValue<float>("float"):F}"); | ||
resultBuilder.AppendLine($"Bool : {reader.GetFieldValue<bool>("bool")}"); | ||
resultBuilder.AppendLine($"Timestamp : {reader.GetFieldValue<DateTime>("timestamp"):u}"); | ||
} | ||
|
||
string result = resultBuilder.ToString(); | ||
Console.WriteLine(result); | ||
return result; | ||
} | ||
} | ||
// [END spanner_postgresql_cast_data_type] |
42 changes: 42 additions & 0 deletions
42
spanner/api/Spanner.Samples/ConnectToDatabaseAsyncPostgre.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,42 @@ | ||
// Copyright 2022 Google Inc. | ||
// | ||
// 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_connect_postgresql_database] | ||
using Google.Cloud.Spanner.Data; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
public class ConnectToDatabaseAsyncPostgreSample | ||
{ | ||
public async Task<string> ConnectToDatabaseAsyncPostgre(string projectId, string instanceId, string databaseId) | ||
{ | ||
string connectionString = $"Data Source=projects/{projectId}/instances/{instanceId}/databases/{databaseId}"; | ||
|
||
using var connection = new SpannerConnection(connectionString); | ||
await connection.OpenAsync(); | ||
|
||
var command = connection.CreateSelectCommand("SELECT 'Hello from Spanner PostgreSQL!'"); | ||
|
||
using var reader = await command.ExecuteReaderAsync(); | ||
string result = default; | ||
while (await reader.ReadAsync()) | ||
{ | ||
result = $"{reader.GetString(0)}"; | ||
} | ||
|
||
Console.WriteLine(result); | ||
return result; | ||
} | ||
} | ||
// [END spanner_connect_postgresql_database] |
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Google.Cloud.Spanner.Data" Version="3.13.0" /> | ||
<PackageReference Include="Google.Cloud.Spanner.Data" Version="3.14.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.