|
15 | 15 | * limitations under the License. |
16 | 16 | */ |
17 | 17 |
|
| 18 | +declare(strict_types=1); |
| 19 | + |
18 | 20 | namespace Google\Cloud\Samples\CloudSQL\SQLServer; |
19 | 21 |
|
20 | 22 | use PDO; |
| 23 | +use PDOException; |
| 24 | +use RuntimeException; |
21 | 25 |
|
| 26 | +/** |
| 27 | + * Manage votes using the Cloud SQL database. |
| 28 | + */ |
22 | 29 | class Votes |
23 | 30 | { |
| 31 | + /** |
| 32 | + * @var PDO |
| 33 | + */ |
24 | 34 | private $connection; |
25 | 35 |
|
| 36 | + /** |
| 37 | + * @param PDO $connection A connection to the database. |
| 38 | + */ |
26 | 39 | public function __construct(PDO $connection) |
27 | 40 | { |
28 | 41 | $this->connection = $connection; |
29 | | - $this->create_table(); |
30 | 42 | } |
31 | 43 |
|
32 | | - private function create_table() |
| 44 | + /** |
| 45 | + * Creates the table if it does not yet exist. |
| 46 | + * |
| 47 | + * @return void |
| 48 | + */ |
| 49 | + public function createTableIfNotExists() |
33 | 50 | { |
34 | | - $tableName = "votes"; |
35 | | - |
36 | 51 | $existsStmt = "SELECT * FROM INFORMATION_SCHEMA.TABLES |
37 | 52 | WHERE TABLE_NAME = ?"; |
38 | 53 |
|
39 | 54 | $stmt = $this->connection->prepare($existsStmt); |
40 | | - $stmt->execute([$tableName]); |
| 55 | + $stmt->execute(['votes']); |
41 | 56 |
|
42 | | - // If table does not exist, create it! |
43 | 57 | $row = $stmt->fetch(PDO::FETCH_ASSOC); |
| 58 | + |
| 59 | + // If the table does not exist, create it. |
44 | 60 | if (!$row) { |
45 | | - $sql = " |
46 | | - CREATE TABLE votes ( |
| 61 | + $sql = "CREATE TABLE votes ( |
47 | 62 | vote_id INT NOT NULL IDENTITY, |
48 | 63 | time_cast DATETIME NOT NULL, |
49 | | - candidate VARCHAR(6) NOT NULL, |
| 64 | + vote_value VARCHAR(6) NOT NULL, |
50 | 65 | PRIMARY KEY (vote_id) |
51 | 66 | );"; |
52 | | - if ($this->connection->exec($sql) !== 1) { |
53 | | - print_r($this->connection->errorInfo()); |
54 | | - exit; |
55 | | - } |
| 67 | + |
| 68 | + $this->connection->exec($sql); |
56 | 69 | } |
57 | 70 | } |
58 | 71 |
|
59 | | - public function list() |
| 72 | + /** |
| 73 | + * Returns a list of the last five votes |
| 74 | + * |
| 75 | + * @return array |
| 76 | + */ |
| 77 | + public function listVotes() : array |
60 | 78 | { |
61 | | - $sql = "SELECT TOP 5 candidate, time_cast FROM votes ORDER BY time_cast DESC"; |
| 79 | + $sql = "SELECT TOP 5 vote_value, time_cast FROM votes ORDER BY time_cast DESC"; |
62 | 80 | $statement = $this->connection->prepare($sql); |
63 | 81 | $statement->execute(); |
64 | | - return $statement->fetchAll(); |
| 82 | + return $statement->fetchAll(PDO::FETCH_ASSOC); |
65 | 83 | } |
66 | 84 |
|
67 | | - public function count_candidates() |
| 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 |
68 | 92 | { |
69 | | - $sql = "SELECT COUNT(vote_id) as voteCount FROM votes WHERE candidate = ?"; |
70 | | - $count = []; |
| 93 | + $sql = "SELECT COUNT(vote_id) as voteCount FROM votes WHERE vote_value = ?"; |
71 | 94 |
|
72 | 95 | $statement = $this->connection->prepare($sql); |
| 96 | + $statement->execute([$value]); |
73 | 97 |
|
74 | | - //tabs |
75 | | - $statement->execute(['TABS']); |
76 | | - $count['tabs'] = $statement->fetch()[0]; |
77 | | - |
78 | | - //spaces |
79 | | - $statement->execute(['SPACES']); |
80 | | - $count['spaces'] = $statement->fetch()[0]; |
81 | | - |
82 | | - return $count; |
| 98 | + return (int) $statement->fetch(PDO::FETCH_COLUMN); |
83 | 99 | } |
84 | 100 |
|
85 | | - public function save($team) |
| 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 |
86 | 108 | { |
87 | | - $sql = "INSERT INTO votes (time_cast, candidate) VALUES (GETDATE(), :candidate)"; |
88 | | - $statement = $this->connection->prepare($sql); |
89 | | - $statement->bindParam('candidate', $team); |
90 | | - |
91 | | - if ($statement->execute()) { |
92 | | - return "Vote successfully cast for '$team'"; |
| 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 | + ); |
93 | 128 | } |
| 129 | + # [END cloud_sql_sqlserver_pdo_connection] |
94 | 130 |
|
95 | | - return print_r($statement->errorInfo(), true); |
| 131 | + return $res; |
96 | 132 | } |
97 | 133 | } |
0 commit comments