|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2020 Google LLC. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +declare(strict_types=1); |
| 19 | + |
| 20 | +namespace Google\Cloud\Samples\CloudSQL\SQLServer; |
| 21 | + |
| 22 | +use PDO; |
| 23 | +use PDOException; |
| 24 | +use RuntimeException; |
| 25 | + |
| 26 | +/** |
| 27 | + * Manage votes using the Cloud SQL database. |
| 28 | + */ |
| 29 | +class Votes |
| 30 | +{ |
| 31 | + /** |
| 32 | + * @var PDO |
| 33 | + */ |
| 34 | + private $connection; |
| 35 | + |
| 36 | + /** |
| 37 | + * @param PDO $connection A connection to the database. |
| 38 | + */ |
| 39 | + public function __construct(PDO $connection) |
| 40 | + { |
| 41 | + $this->connection = $connection; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Creates the table if it does not yet exist. |
| 46 | + * |
| 47 | + * @return void |
| 48 | + */ |
| 49 | + public function createTableIfNotExists() |
| 50 | + { |
| 51 | + $existsStmt = "SELECT * FROM INFORMATION_SCHEMA.TABLES |
| 52 | + WHERE TABLE_NAME = ?"; |
| 53 | + |
| 54 | + $stmt = $this->connection->prepare($existsStmt); |
| 55 | + $stmt->execute(['votes']); |
| 56 | + |
| 57 | + $row = $stmt->fetch(PDO::FETCH_ASSOC); |
| 58 | + |
| 59 | + // If the table does not exist, create it. |
| 60 | + if (!$row) { |
| 61 | + $sql = "CREATE TABLE votes ( |
| 62 | + vote_id INT NOT NULL IDENTITY, |
| 63 | + time_cast DATETIME NOT NULL, |
| 64 | + vote_value VARCHAR(6) NOT NULL, |
| 65 | + PRIMARY KEY (vote_id) |
| 66 | + );"; |
| 67 | + |
| 68 | + $this->connection->exec($sql); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Returns a list of the last five votes |
| 74 | + * |
| 75 | + * @return array |
| 76 | + */ |
| 77 | + public function listVotes() : array |
| 78 | + { |
| 79 | + $sql = "SELECT TOP 5 vote_value, time_cast FROM votes ORDER BY time_cast DESC"; |
| 80 | + $statement = $this->connection->prepare($sql); |
| 81 | + $statement->execute(); |
| 82 | + return $statement->fetchAll(PDO::FETCH_ASSOC); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Get the number of votes cast for a given value. |
| 87 | + * |
| 88 | + * @param string $value |
| 89 | + * @param int |
| 90 | + */ |
| 91 | + public function getCountByValue(string $value) : int |
| 92 | + { |
| 93 | + $sql = "SELECT COUNT(vote_id) as voteCount FROM votes WHERE vote_value = ?"; |
| 94 | + |
| 95 | + $statement = $this->connection->prepare($sql); |
| 96 | + $statement->execute([$value]); |
| 97 | + |
| 98 | + return (int) $statement->fetch(PDO::FETCH_COLUMN); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Insert a new vote into the database |
| 103 | + * |
| 104 | + * @param string $value The value to vote for. |
| 105 | + * @return boolean |
| 106 | + */ |
| 107 | + public function insertVote(string $value) : bool |
| 108 | + { |
| 109 | + $conn = $this->connection; |
| 110 | + $res = false; |
| 111 | + |
| 112 | + # [START cloud_sql_sqlserver_pdo_connection] |
| 113 | + // Use prepared statements to guard against SQL injection. |
| 114 | + $sql = "INSERT INTO votes (time_cast, vote_value) VALUES (GETDATE(), :voteValue)"; |
| 115 | + |
| 116 | + try { |
| 117 | + $statement = $conn->prepare($sql); |
| 118 | + $statement->bindParam('voteValue', $value); |
| 119 | + |
| 120 | + $res = $statement->execute(); |
| 121 | + } catch (PDOException $e) { |
| 122 | + throw new RuntimeException( |
| 123 | + "Could not insert vote into database. The PDO exception was " . |
| 124 | + $e->getMessage(), |
| 125 | + $e->getCode(), |
| 126 | + $e |
| 127 | + ); |
| 128 | + } |
| 129 | + # [END cloud_sql_sqlserver_pdo_connection] |
| 130 | + |
| 131 | + return $res; |
| 132 | + } |
| 133 | +} |
0 commit comments