Skip to content

Commit d351c02

Browse files
Merge pull request #5 from swiftlydesigner/2-connect-to-db-with-basic-operations
2 connect to db with basic operations
2 parents a5d2042 + 1fdca76 commit d351c02

File tree

102 files changed

+6501
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+6501
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.11.35327.3
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PA 8", "PA 8\PA 8.vcxproj", "{D8F947FC-58E4-452F-BEC2-C0B905D238B6}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x64 = Debug|x64
11+
Debug|x86 = Debug|x86
12+
Release|x64 = Release|x64
13+
Release|x86 = Release|x86
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{D8F947FC-58E4-452F-BEC2-C0B905D238B6}.Debug|x64.ActiveCfg = Debug|x64
17+
{D8F947FC-58E4-452F-BEC2-C0B905D238B6}.Debug|x64.Build.0 = Debug|x64
18+
{D8F947FC-58E4-452F-BEC2-C0B905D238B6}.Debug|x86.ActiveCfg = Debug|Win32
19+
{D8F947FC-58E4-452F-BEC2-C0B905D238B6}.Debug|x86.Build.0 = Debug|Win32
20+
{D8F947FC-58E4-452F-BEC2-C0B905D238B6}.Release|x64.ActiveCfg = Release|x64
21+
{D8F947FC-58E4-452F-BEC2-C0B905D238B6}.Release|x64.Build.0 = Release|x64
22+
{D8F947FC-58E4-452F-BEC2-C0B905D238B6}.Release|x86.ActiveCfg = Release|Win32
23+
{D8F947FC-58E4-452F-BEC2-C0B905D238B6}.Release|x86.Build.0 = Release|Win32
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {FF09BBAC-5D60-476F-B3BE-58FC631E615C}
30+
EndGlobalSection
31+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//
2+
// BusinessReviewAnalyzer.cpp
3+
// PostgreSQL
4+
//
5+
// Created by Kyle Parker on 10/18/24.
6+
//
7+
8+
/// TODO: Implement BusinessReviewAnalyzer!
9+
10+
#include "BusinessReviewAnalyzer.hpp"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// BusinessReviewAnalyzer.hpp
3+
// PostgreSQL
4+
//
5+
// Created by Kyle Parker on 10/18/24.
6+
//
7+
8+
/// TODO: Design BusinessReviewAnalyzer!
9+
10+
#ifndef BusinessReviewAnalyzer_hpp
11+
#define BusinessReviewAnalyzer_hpp
12+
13+
#include "../Entities/GoogleReviewEntity.hpp"
14+
15+
using std::vector;
16+
17+
class BusinessReviewAnalyzer final {
18+
vector<GoogleReviewEntity> _entites;
19+
20+
public:
21+
BusinessReviewAnalyzer(const vector<GoogleReviewEntity>);
22+
23+
/// TODO: Add your functions for analysis here!
24+
};
25+
26+
#endif /* BusinessReviewAnalyzer_hpp */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Reviewer.hpp
3+
// PostgreSQL
4+
//
5+
// Created by Kyle Parker on 10/29/24.
6+
//
7+
8+
#ifndef Reviewer_hpp
9+
#define Reviewer_hpp
10+
11+
#include <vector>
12+
13+
using std::vector;
14+
15+
template <class T>
16+
class Reviewer {
17+
vector<T> _entities;
18+
19+
public:
20+
Reviewer();
21+
Reviewer(const vector<T>& entities);
22+
23+
Reviewer(const Reviewer& copy);
24+
Reviewer operator=(const Reviewer& rhs);
25+
26+
virtual ~Reviewer();
27+
28+
virtual vector<T> entities();
29+
30+
virtual T getMax() = 0;
31+
virtual T getMin() = 0;
32+
};
33+
34+
#endif /* Reviewer_hpp */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Files to Modify
2+
TLDR: Look for TODOs throughout the program
3+
1. Analysis/
4+
1.1 Please inherit from Reviewer and be sure to specialize the inheritee
5+
2. Tree/Tree.hpp
6+
3. Tree/Tree.cpp
7+
4. Tree/TreeNode.cpp
8+
5. Tree/TreeNode.cpp
9+
6. ReviewApp.hpp
10+
7. ReviewApp.cpp
11+
8. Files you create?
12+
13+
# Files to Look at, but Don't Modify
14+
Be sure to look at the files below:
15+
1. Anything under Entites, Database, or Types
16+
2. LoginController.hpp
17+
3. All other functions in LoginController.cpp
18+
4. NodeData.hpp
19+
5. NodeData.cpp
20+
6. TreeBase.hpp
21+
7. main.cpp
22+
23+
# Instructions
24+
See Canvas for PA instructions, requirements, and grading rubric.
25+
26+
# Users
27+
## Single Country (Country code is the login)
28+
1. username = US, password = US
29+
2. username = GB, password = GB
30+
3. username = DE, password = DE
31+
4. username = CA, password = CA
32+
5. username = BR, password = BR
33+
6. username = FR, password = FR
34+
7. username = AU, password = AU
35+
36+
## Multi Country
37+
1. username = user, password = password
38+
39+
# Install
40+
See README.md.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//
2+
// DbRepository.cpp
3+
// PostgreSQL
4+
//
5+
// Created by Kyle Parker on 10/3/24.
6+
//
7+
8+
/// Attention students: Nothing to do here!
9+
10+
#include <iostream>
11+
12+
#include "DbRepository.hpp"
13+
14+
/// Construct a controller with a given filled out URL
15+
/// @Param fullURL The complete server URL for connecting to
16+
DbRepository::DbRepository(const string& fullURL) {
17+
this->url = fullURL;
18+
}
19+
20+
/// Cleanup before destroying obejct
21+
DbRepository::~DbRepository() {
22+
23+
}
24+
25+
/// Select all rows from a given table
26+
/// @Param table The table to read everything from
27+
pqxx::result DbRepository::selectAllFrom(const string& table) const {
28+
pqxx::result results = pqxx::result();
29+
30+
try {
31+
// Connect to the database
32+
pqxx::connection conn(url);
33+
34+
// Check if the connection is open
35+
#ifdef DEBUG
36+
if (conn.is_open()) {
37+
std::cout << "Opened database successfully: " << conn.dbname() << std::endl;
38+
} else {
39+
std::cout << "Can't open database" << std::endl;
40+
return results;
41+
}
42+
#else
43+
if (!conn.is_open()) {
44+
return results;
45+
}
46+
#endif
47+
48+
// Create a non-transactional object
49+
pqxx::work trans(conn);
50+
51+
// Execute a SQL query
52+
results = trans.exec("SELECT * FROM " + table + ";");
53+
54+
// Print the result
55+
return results;
56+
57+
} catch (const pqxx::sql_error &e) {
58+
std::cerr << "SQL error: " << e.what() << std::endl;
59+
std::cerr << "Query: " << e.query() << std::endl;
60+
} catch (const std::exception &e) {
61+
std::cerr << e.what() << std::endl;
62+
}
63+
return results;
64+
}
65+
66+
/// Select all fields from a given table
67+
/// @Param fields The fields to select in
68+
/// @Param table The table to read from
69+
/// @Param where Conditionals
70+
pqxx::result DbRepository::selectWhere(const string& fields, const string& table, const string& where) const {
71+
pqxx::result results;
72+
73+
try {
74+
75+
// Connect to the database
76+
pqxx::connection conn(url);
77+
78+
// Check if the connection is open
79+
#ifdef DEBUG
80+
if (conn.is_open()) {
81+
std::cout << "Opened database successfully: " << conn.dbname() << std::endl;
82+
} else {
83+
std::cout << "Can't open database" << std::endl;
84+
return results;
85+
}
86+
#else
87+
if (!conn.is_open()) {
88+
return results;
89+
}
90+
#endif
91+
92+
// Create a transactional object
93+
pqxx::work trans(conn);
94+
95+
// Execute a SQL query
96+
results = trans.exec("SELECT " + fields + " FROM " + table + " WHERE " + where + ";");
97+
98+
} catch (const pqxx::sql_error &e) {
99+
std::cerr << "SQL error: " << e.what() << std::endl;
100+
std::cerr << "Query: " << e.query() << std::endl;
101+
} catch (const std::exception &e) {
102+
std::cerr << "Exception: " << e.what() << std::endl;
103+
}
104+
return results;
105+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// DbRepository.hpp
3+
// PostgreSQL
4+
//
5+
// Created by Kyle Parker on 10/3/24.
6+
//
7+
8+
/// Attention students: Nothing to do here!
9+
10+
#ifndef DbRepository_hpp
11+
#define DbRepository_hpp
12+
13+
#include <string>
14+
#include <pqxx/pqxx>
15+
16+
using std::string;
17+
18+
class DbRepository {
19+
/// Disable copy constructor
20+
DbRepository(DbRepository& copy) = delete;
21+
/// Disable = operator
22+
DbRepository operator=(const DbRepository&rhs) = delete;
23+
24+
string url;
25+
26+
public:
27+
/// Construct a controller with a given filled out URL
28+
/// @Param fullURL The complete server URL for connecting to
29+
DbRepository(const string& fullURL);
30+
31+
/// Cleanup before destroying obejct
32+
~DbRepository();
33+
34+
/// Select all rows from a given table
35+
pqxx::result selectAllFrom(const string& table) const;
36+
37+
/// Select a fields form a row where the conditions are met from the given table
38+
pqxx::result selectWhere(const string& fields, const string& table, const string& where) const;
39+
};
40+
41+
#endif /* DbRepository_hpp */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// DbService.cpp
3+
// PostgreSQL
4+
//
5+
// Created by Kyle Parker on 10/17/24.
6+
//
7+
8+
/// Attention students: Nothing to do here!
9+
10+
#include "DbService.hpp"
11+
12+
/// Create a service item to access a database
13+
/// @Param serverAddress The URL, containing auth info for target DB
14+
DbService::DbService(const string& serverAddress) : _repo(serverAddress) {
15+
16+
}
17+
18+
/// Get all reviews in the database
19+
vector<GoogleReviewEntity> DbService::getAllReviews() {
20+
vector<GoogleReviewEntity> entities = vector<GoogleReviewEntity>();
21+
22+
pqxx::result res = _repo.selectAllFrom("google_reviews");
23+
24+
for (pqxx::row row : res) {
25+
entities.push_back(GoogleReviewEntity(row));
26+
}
27+
28+
return entities;
29+
}
30+
31+
/// Get reviews in the database with the constraints
32+
/// @Param country The country to filter by
33+
vector<GoogleReviewEntity> DbService::getReviewsWithCountry(Country country) {
34+
vector<GoogleReviewEntity> entities = vector<GoogleReviewEntity>();
35+
36+
pqxx::result res = _repo.selectWhere("*", "google_reviews", "country = '" + countryToString(country) + "'");
37+
38+
for (pqxx::row row : res) {
39+
entities.push_back(GoogleReviewEntity(row));
40+
}
41+
42+
return entities;
43+
}
44+
45+
/// Get the login info for the specified username.
46+
/// @Param username The username to login with
47+
QueryResultVector DbService::getLoginDataForUser(const std::string& username) {
48+
QueryResultVector data = {};
49+
50+
pqxx::result result = this->_repo.selectWhere("password, countries", "google_login", "username = '" + username + "'");
51+
52+
const pqxx::row& row = result.front();
53+
54+
data.push_back(make_pair("password", row["password"].as< optional<string> >()));
55+
data.push_back(make_pair("country", row["countries"].as< optional<string> >()));
56+
data.push_back(make_pair("usr", username));
57+
58+
return data;
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// DbService.hpp
3+
// PostgreSQL
4+
//
5+
// Created by Kyle Parker on 10/17/24.
6+
//
7+
8+
/// Attention students: Nothing to do here!
9+
10+
#ifndef DbService_hpp
11+
#define DbService_hpp
12+
13+
#include <vector>
14+
15+
#include "DbRepository.hpp"
16+
#include "../Entities/GoogleReviewEntity.hpp"
17+
18+
using std::vector;
19+
using std::pair;
20+
using std::make_pair;
21+
22+
using QueryResultVector = vector< pair<string, optional<string> > >;
23+
24+
class DbService final {
25+
DbRepository _repo;
26+
27+
public:
28+
/// Create a service item to access a database
29+
/// @Param serverAddress The URL, containing auth info for target DB
30+
DbService(const string& serverAddress);
31+
32+
/// Get all reviews in the database
33+
vector<GoogleReviewEntity> getAllReviews();
34+
35+
/// Get reviews in the database with the constraints
36+
/// @Param country The country to filter by
37+
vector<GoogleReviewEntity> getReviewsWithCountry(Country country);
38+
39+
/// Get the login data in the form of a vector
40+
/// @Param username The username to login under
41+
QueryResultVector getLoginDataForUser(const std::string& username);
42+
};
43+
44+
#endif /* DbService_hpp */

0 commit comments

Comments
 (0)