-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(cloud_sql/sqlserver): update to V2 sample #1636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bshaffer
merged 8 commits into
GoogleCloudPlatform:master
from
jsimonweb:cloud-sql-v2-sqlserver-sample
Jun 3, 2022
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cf11969
feat(cloud_sql/sqlserver): update to V2 sample
jsimonweb 94be590
Address review comments.
jsimonweb 3ae1144
Merge branch 'master' into cloud-sql-v2-sqlserver-sample
jsimonweb 12643cf
Merge branch 'master' into cloud-sql-v2-sqlserver-sample
jsimonweb 7ec78f1
Address review comments.
jsimonweb 1ebe005
Merge branch 'master' into cloud-sql-v2-sqlserver-sample
jsimonweb e502090
Merge branch 'master' into cloud-sql-v2-sqlserver-sample
jsimonweb e3d8f99
Merge branch 'master' into cloud-sql-v2-sqlserver-sample
jsimonweb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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,94 @@ | ||
| <?php | ||
| /* | ||
| * Copyright 2020 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. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| # [START cloud_sql_sqlserver_pdo_connect_tcp] | ||
| namespace Google\Cloud\Samples\CloudSQL\SQLServer; | ||
|
|
||
| use PDO; | ||
| use PDOException; | ||
| use RuntimeException; | ||
| use TypeError; | ||
|
|
||
| class DatabaseTcp | ||
| { | ||
| public static function initTcpDatabaseConnection(): PDO | ||
| { | ||
| try { | ||
| // Note: Saving credentials in environment variables is convenient, but not | ||
| // secure - consider a more secure solution such as | ||
| // Cloud Secret Manager (https://cloud.google.com/secret-manager) to help | ||
| // keep secrets safe. | ||
| $username = getenv('DB_USER'); // e.g. 'your_db_user' | ||
| $password = getenv('DB_PASS'); // e.g. 'your_db_password' | ||
| $dbName = getenv('DB_NAME'); // e.g. 'your_db_name' | ||
| $instanceHost = getenv('INSTANCE_HOST'); // e.g. '127.0.0.1' ('172.17.0.1' for GAE Flex) | ||
|
|
||
| // Connect using TCP | ||
| $dsn = sprintf( | ||
| 'sqlsrv:server=%s;Database=%s', | ||
| $instanceHost, | ||
| $dbName | ||
| ); | ||
|
|
||
| // Connect to the database | ||
| $conn = new PDO( | ||
| $dsn, | ||
| $username, | ||
| $password, | ||
| # [START_EXCLUDE] | ||
| # [START cloud_sql_sqlserver_pdo_timeout] | ||
| // Here we set the connection timeout to five seconds and ask PDO to | ||
| // throw an exception if any errors occur. | ||
| [ | ||
| PDO::ATTR_TIMEOUT => 5, | ||
| PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, | ||
| ] | ||
| # [END cloud_sql_sqlserver_pdo_timeout] | ||
| # [END_EXCLUDE] | ||
| ); | ||
| } catch (TypeError $e) { | ||
| throw new RuntimeException( | ||
| sprintf( | ||
| 'Invalid or missing configuration! Make sure you have set ' . | ||
| '$username, $password, $dbName, and $instanceHost (for TCP mode). ' . | ||
| 'The PHP error was %s', | ||
| $e->getMessage() | ||
| ), | ||
| $e->getCode(), | ||
| $e | ||
| ); | ||
| } catch (PDOException $e) { | ||
| throw new RuntimeException( | ||
| sprintf( | ||
| 'Could not connect to the Cloud SQL Database. Check that ' . | ||
| 'your username and password are correct, that the Cloud SQL ' . | ||
| 'proxy is running, and that the database exists and is ready ' . | ||
| 'for use. For more assistance, refer to %s. The PDO error was %s', | ||
| 'https://cloud.google.com/sql/docs/sqlserver/connect-external-app', | ||
| $e->getMessage() | ||
| ), | ||
| (int) $e->getCode(), | ||
| $e | ||
| ); | ||
| } | ||
|
|
||
| return $conn; | ||
| } | ||
| } | ||
| # [END cloud_sql_sqlserver_pdo_connect_tcp] | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.