Commit 8cc3e905 by arun.uday

v5

parent e1eb9c50
# Exception constant
exception_msg = "Exception occurred"
class ConstantsValues:
# Exception constant
exception_msg = "Exception occurred"
# column names
columns_names = ['product_id', 'product_name', 'product_category', 'retail_price',
'discount_price', 'description', 'overall_rating']
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
import psycopg2 as py import psycopg2 as py
from scripts.config.postgres_connect_config import host_name, port, dbname, user, password from scripts.config.postgres_connect_config import host_name, port, dbname, user, password
from scripts.constants.file_constants import exception_msg from scripts.constants.proj_constants import ConstantsValues
from scripts.core.database.postgres.queries.post_queries import QueriesPost
try: try:
postgres_client = py.connect(host=host_name, port=port, user=user, password=password) postgres_client = py.connect(host=host_name, port=port, user=user, password=password)
...@@ -12,7 +12,7 @@ try: ...@@ -12,7 +12,7 @@ try:
# creating cursor for the postgres # creating cursor for the postgres
cur = postgres_client.cursor() cur = postgres_client.cursor()
cur.execute("SELECT datname FROM pg_database WHERE datistemplate = false;") cur.execute(QueriesPost().databases())
# Fetch the result # Fetch the result
result = cur.fetchall() result = cur.fetchall()
...@@ -27,4 +27,4 @@ try: ...@@ -27,4 +27,4 @@ try:
postgres_client.commit() postgres_client.commit()
cur.execute(f'CREATE DATABASE {dbname}') cur.execute(f'CREATE DATABASE {dbname}')
except Exception as e: except Exception as e:
print(f'{exception_msg},{e}') print(f'{ConstantsValues().exception_msg},{e}')
...@@ -3,20 +3,25 @@ from scripts.config.postgres_connect_config import products ...@@ -3,20 +3,25 @@ from scripts.config.postgres_connect_config import products
class QueriesPost: class QueriesPost:
@staticmethod @staticmethod
def databases():
db = "SELECT datname FROM pg_database WHERE datistemplate = false;"
return db
@staticmethod
def create_table(): def create_table():
table = f"CREATE TABLE if not exists {products} " \ table = f"CREATE TABLE if not exists {products} " \
f"(Product_Id SERIAL PRIMARY KEY, Product_Name VARCHAR(255)," \ f"(product_id SERIAL PRIMARY KEY, product_name VARCHAR(255)," \
f"Product_Category VARCHAR(255)," \ f"product_category VARCHAR(255)," \
f"Retail_Price INT," \ f"retail_price INT," \
f"Discounted_Price INT," \ f"discounted_price INT," \
f"Description VARCHAR(255)," \ f"description VARCHAR(255)," \
f"Overall_Rating INT)" f"overall_rating INT)"
return table return table
@staticmethod @staticmethod
def insert_data(): def insert_data():
insert_data = f"INSERT INTO {products} (Product_Name, Product_Category, Retail_Price, Discounted_Price," \ insert_data = f"INSERT INTO {products} (product_name, product_category, retail_price, discounted_price," \
f"Description, Overall_Rating) VALUES (%s, %s, %s, %s, %s, %s)" f"description, overall_rating) VALUES (%s, %s, %s, %s, %s, %s)"
return insert_data return insert_data
@staticmethod @staticmethod
...@@ -31,10 +36,10 @@ class QueriesPost: ...@@ -31,10 +36,10 @@ class QueriesPost:
@staticmethod @staticmethod
def fetch_all(): def fetch_all():
fetch_data = f"SELECT * FROM {products}" fetch_data = f"SELECT * FROM {products} ORDER BY product_id ASC"
return fetch_data return fetch_data
@staticmethod @staticmethod
def delete_data(field_name, del_data): def delete_data(del_data):
del_val = f"DELETE FROM {products} WHERE {field_name} = {del_data}" del_val = f"DELETE FROM {products} WHERE {del_data}"
return del_val return del_val
...@@ -9,7 +9,7 @@ def read_inputs(): ...@@ -9,7 +9,7 @@ def read_inputs():
retail_price = int(input("Enter the retail price: ")) retail_price = int(input("Enter the retail price: "))
discount_price = int(input("Enter the discounted price: ")) discount_price = int(input("Enter the discounted price: "))
description = input("Enter the product description: ") description = input("Enter the product description: ")
over_rating = input("Enter the overall rating") over_rating = int(input("Enter the overall rating"))
return p_id, p_name, p_category, retail_price, discount_price, description, over_rating return p_id, p_name, p_category, retail_price, discount_price, description, over_rating
...@@ -17,8 +17,8 @@ def read_inputs(): ...@@ -17,8 +17,8 @@ def read_inputs():
# inserting data to mongo # inserting data to mongo
def insert_data_db(db, p_id, p_name, p_category, retail_price, discount_price, description, over_rating): def insert_data_db(db, p_id, p_name, p_category, retail_price, discount_price, description, over_rating):
try: try:
insert_res = MongoDBQuery().insert(db, p_id, p_name, p_category, retail_price, discount_price, description, over_rating) insert_res = MongoDBQuery().insert(db, p_id, p_name, p_category, retail_price, discount_price, description,
over_rating)
return insert_res return insert_res
except Exception as e: except Exception as e:
print("Exception occurred :", e) print("Exception occurred :", e)
# deleting data # deleting data
# getting user inputs # getting user inputs
from scripts.constants.proj_constants import ConstantsValues
from scripts.core.database.mongo.queries.mongodb_queires import MongoDBQuery from scripts.core.database.mongo.queries.mongodb_queires import MongoDBQuery
def delete_db_input(): def delete_db_input():
columns_ = ['product_id', 'product_name', 'product_category', 'retail_price', columns_ = ConstantsValues().columns_names
'discount_price', 'description', 'overall_rating']
integer_val = [0, 3, 4, 6] integer_val = [0, 3, 4, 6]
counter = 0 counter = 0
condition_values = {} condition_values = {}
......
from scripts.config.postgres_connect_config import products
from scripts.core.database.postgres.postgres_connect import cur from scripts.core.database.postgres.postgres_connect import cur
from scripts.core.database.postgres.queries.post_queries import QueriesPost
def post_columns(): def post_columns():
# Execute a SELECT statement to get the column names of a table # Execute a SELECT statement to get the column names of a table
cur.execute(f"SELECT * FROM {products} LIMIT 0") cur.execute(QueriesPost().fetch_all())
# Get the column names # Get the column names
field_names = [desc[0] for desc in cur.description] field_names = [desc[0] for desc in cur.description]
......
# class for mongodb operations # class for mongodb operations
from scripts.constants.file_constants import exception_msg from scripts.config.postgres_connect_config import dbname, products
from scripts.constants.proj_constants import ConstantsValues
from scripts.core.database.mongo.mongodb_connect import client from scripts.core.database.mongo.mongodb_connect import client
from scripts.core.handlers.data_insert import insert_data_db, read_inputs from scripts.core.handlers.data_insert import insert_data_db, read_inputs
from scripts.core.handlers.delete_db_data import delete_db_input, db_delete from scripts.core.handlers.delete_db_data import delete_db_input, db_delete
...@@ -16,8 +17,8 @@ class MongoDbStart: ...@@ -16,8 +17,8 @@ class MongoDbStart:
except Exception as e: except Exception as e:
print("Connection failed: ", e) print("Connection failed: ", e)
else: else:
db = client_con['storeManager'] db = client_con[dbname]
store_manager = db['products'] store_manager = db[products]
return store_manager return store_manager
# mongo data insert # mongo data insert
...@@ -49,7 +50,7 @@ class MongoDbStart: ...@@ -49,7 +50,7 @@ class MongoDbStart:
else: else:
print("Data not updated") print("Data not updated")
except Exception as e: except Exception as e:
print(f'{exception_msg}{e}') print(f'{ConstantsValues().exception_msg}{e}')
# mongo delete data # mongo delete data
@staticmethod @staticmethod
...@@ -62,7 +63,7 @@ class MongoDbStart: ...@@ -62,7 +63,7 @@ class MongoDbStart:
else: else:
print("Data not deleted") print("Data not deleted")
except Exception as e: except Exception as e:
print(f'{exception_msg}{e}') print(f'{ConstantsValues().exception_msg}{e}')
# mongo view data # mongo view data
@staticmethod @staticmethod
...@@ -72,6 +73,4 @@ class MongoDbStart: ...@@ -72,6 +73,4 @@ class MongoDbStart:
view_fields(db, field_filter) view_fields(db, field_filter)
except Exception as e: except Exception as e:
print(f'{exception_msg}{e}') print(f'{ConstantsValues().exception_msg}{e}')
# deleting data # deleting data
# getting user inputs # getting user inputs
from scripts.config.postgres_connect_config import products from scripts.constants.proj_constants import ConstantsValues
from scripts.core.database.postgres.queries.post_queries import QueriesPost from scripts.core.database.postgres.queries.post_queries import QueriesPost
# user input # user input
def delete_db_post(): def delete_db_post():
field_name = input("Enter the field condition: ") columns_ = ConstantsValues().columns_names
del_data = int(input("Enter the data: ")) integer_val = [0, 3, 4, 6]
counter = 0
condition_values = {}
print("Conditions")
while counter < len(columns_):
field_value = input(f"Enter the {columns_[counter]} data: ")
if field_value != '' and (counter in integer_val):
condition_values.update({f'{columns_[counter]}': int(field_value)})
if field_value != '' and (counter not in integer_val):
condition_values.update({f'{columns_[counter]}': f'{field_value}'})
counter += 1
return field_name, del_data return condition_values
# deleting the record # deleting the record
def db_delete_post(cur, field_name, del_data): def db_delete_post(cur, condition_values):
return cur.execute(QueriesPost().delete_data(field_name, del_data)) conditions = [f"{field} = '{value}'" for field, value in condition_values.items()]
conditions = "and ".join(conditions)
if conditions:
cur = cur.execute(QueriesPost().delete_data(condition_values))
else:
cur = None
return cur
# reading user inputs # reading user inputs
from scripts.config.postgres_connect_config import products from scripts.constants.proj_constants import ConstantsValues
from scripts.constants.file_constants import exception_msg
from scripts.core.database.postgres.queries.post_queries import QueriesPost from scripts.core.database.postgres.queries.post_queries import QueriesPost
# user inputs # user inputs
def read_inputs_post(): def read_inputs_post():
p_id = int(input("Enter the product id: "))
p_name = input("Enter the product name: ") p_name = input("Enter the product name: ")
p_category = input("Enter the category: ") p_category = input("Enter the category: ")
retail_price = int(input("Enter the retail price: ")) retail_price = int(input("Enter the retail price: "))
...@@ -14,7 +12,7 @@ def read_inputs_post(): ...@@ -14,7 +12,7 @@ def read_inputs_post():
description = input("Enter the product description: ") description = input("Enter the product description: ")
over_rating = input("Enter the overall rating") over_rating = input("Enter the overall rating")
return p_id, p_name, p_category, retail_price, discount_price, description, over_rating return p_name, p_category, retail_price, discount_price, description, over_rating
# insert in to table # insert in to table
...@@ -23,7 +21,7 @@ def insert_data_post(cur, p_name, p_category, retail_price, discount_price, desc ...@@ -23,7 +21,7 @@ def insert_data_post(cur, p_name, p_category, retail_price, discount_price, desc
val_data = [p_name, p_category, retail_price, discount_price, description, over_rating] val_data = [p_name, p_category, retail_price, discount_price, description, over_rating]
cur.execute(QueriesPost().insert_data(), val_data) cur.execute(QueriesPost().insert_data(), val_data)
except Exception as e: except Exception as e:
print(f'{exception_msg},{e}') print(f'{ConstantsValues().exception_msg},{e}')
return False return False
else: else:
return True return True
# updating the db # updating the db
from scripts.config.postgres_connect_config import products from scripts.constants.proj_constants import ConstantsValues
from scripts.core.database.postgres.queries.post_queries import QueriesPost from scripts.core.database.postgres.queries.post_queries import QueriesPost
from scripts.core.handlers.get_post_columns import post_columns
# user data # user data
def input_update_post(): def input_update_post():
columns_ = post_columns() columns_ = ConstantsValues().columns_names
counter = 0 counter = 0
condition_values = {} condition_values = {}
print("Conditions") print("Conditions")
...@@ -35,7 +34,7 @@ def db_update_post(cur, condition_values, update_values): ...@@ -35,7 +34,7 @@ def db_update_post(cur, condition_values, update_values):
updates = [f"{field} = '{value}'" for field, value in update_values.items()] updates = [f"{field} = '{value}'" for field, value in update_values.items()]
updates = ", ".join(updates) updates = ", ".join(updates)
if conditions and updates: if conditions and updates:
cur = cur.execute(QueriesPost().update_data()) cur = cur.execute(QueriesPost().update_data(updates, conditions))
else: else:
cur = None cur = None
return cur return cur
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# display conditions # display conditions
import pandas as pd import pandas as pd
from scripts.config.postgres_connect_config import products from scripts.constants.proj_constants import ConstantsValues
from scripts.core.database.postgres.queries.post_queries import QueriesPost from scripts.core.database.postgres.queries.post_queries import QueriesPost
from scripts.core.handlers.get_post_columns import post_columns from scripts.core.handlers.get_post_columns import post_columns
...@@ -27,17 +27,13 @@ def view_fields_post(cur, condition_values): ...@@ -27,17 +27,13 @@ def view_fields_post(cur, condition_values):
conditions = [f"{field} = '{value}'" for field, value in condition_values.items()] conditions = [f"{field} = '{value}'" for field, value in condition_values.items()]
conditions = " and ".join(conditions) conditions = " and ".join(conditions)
if conditions: if conditions:
cur.execute(QueriesPost().fetch_conditions()) print(conditions)
cur.execute(QueriesPost().fetch_conditions(conditions))
cursor_data = cur.fetchall() cursor_data = cur.fetchall()
if cursor_data: if cursor_data:
print("Data collected....") print("Data collected....")
view_data = pd.DataFrame((view_data_record for view_data_record in cursor_data), columns=['Product Id', view_data = pd.DataFrame((view_data_record for view_data_record in cursor_data),
'Product_Name', columns=ConstantsValues().columns_names)
'Product_Category',
'Retail_Price',
'Discounted_Price',
'Description',
'Overall_Rating'])
print(view_data) print(view_data)
else: else:
print("Data fetching failed") print("Data fetching failed")
...@@ -46,13 +42,8 @@ def view_fields_post(cur, condition_values): ...@@ -46,13 +42,8 @@ def view_fields_post(cur, condition_values):
cursor_data = cur.fetchall() cursor_data = cur.fetchall()
if cursor_data: if cursor_data:
print("Data collected....") print("Data collected....")
view_data = pd.DataFrame((view_data_record for view_data_record in cursor_data), columns=['Product Id', view_data = pd.DataFrame((view_data_record for view_data_record in cursor_data),
'Product_Name', columns=ConstantsValues().columns_names)
'Product_Category',
'Retail_Price',
'Discounted_Price',
'Description',
'Overall_Rating'])
print(view_data) print(view_data)
else: else:
print("Data fetching failed") print("Data fetching failed")
# class for postgresSql # class for postgresSql
from scripts.constants.file_constants import exception_msg from scripts.constants.proj_constants import ConstantsValues
from scripts.core.database.postgres.postgres_connect import cur, postgres_client from scripts.core.database.postgres.postgres_connect import cur, postgres_client
from scripts.core.handlers.postgres_data_del import delete_db_post, db_delete_post from scripts.core.handlers.postgres_data_del import delete_db_post, db_delete_post
from scripts.core.handlers.postgres_data_insert import read_inputs_post, insert_data_post from scripts.core.handlers.postgres_data_insert import read_inputs_post, insert_data_post
...@@ -19,14 +19,14 @@ class PostgresStart: ...@@ -19,14 +19,14 @@ class PostgresStart:
else: else:
return check_result return check_result
except Exception as e: except Exception as e:
print(f'{exception_msg}{e}') print(f'{ConstantsValues().exception_msg}{e}')
@staticmethod @staticmethod
def post_insert(): def post_insert():
try: try:
num_prod = int(input("Enter the number of products: ")) num_prod = int(input("Enter the number of products: "))
while num_prod > 0: while num_prod > 0:
p_id, p_name, p_category, retail_price, discount_price, description, over_rating = read_inputs_post() p_name, p_category, retail_price, discount_price, description, over_rating = read_inputs_post()
insert_check = insert_data_post(cur, p_name, insert_check = insert_data_post(cur, p_name,
p_category, retail_price, discount_price, description, over_rating) p_category, retail_price, discount_price, description, over_rating)
if insert_check: if insert_check:
...@@ -47,19 +47,19 @@ class PostgresStart: ...@@ -47,19 +47,19 @@ class PostgresStart:
else: else:
print("Data not updated") print("Data not updated")
except Exception as e: except Exception as e:
print(f'{exception_msg},{e}') print(f'{ConstantsValues().exception_msg},{e}')
@staticmethod @staticmethod
def post_delete(): def post_delete():
try: try:
field_name, del_data = delete_db_post() condition_values = delete_db_post()
del_check = db_delete_post(cur, field_name, del_data) del_check = db_delete_post(cur, condition_values)
if del_check is None: if del_check is None:
print("Data deleted") print("Data deleted")
else: else:
print("Data not deleted") print("Data not deleted")
except Exception as e: except Exception as e:
print(f'{exception_msg}{e}') print(f'{ConstantsValues().exception_msg}{e}')
@staticmethod @staticmethod
def post_view(): def post_view():
...@@ -68,11 +68,10 @@ class PostgresStart: ...@@ -68,11 +68,10 @@ class PostgresStart:
view_fields_post(cur, condition_values) view_fields_post(cur, condition_values)
except Exception as e: except Exception as e:
print(f'{exception_msg}{e}') print(f'{ConstantsValues().exception_msg}{e}')
@staticmethod @staticmethod
def post_quit(): def post_quit():
# Close the cursor and connection # Close the cursor and connection
cur.close() cur.close()
postgres_client.close() postgres_client.close()
# creating a table # creating a table
from scripts.config.postgres_connect_config import products from scripts.constants.proj_constants import ConstantsValues
from scripts.constants.file_constants import exception_msg
from scripts.core.database.postgres.queries.post_queries import QueriesPost from scripts.core.database.postgres.queries.post_queries import QueriesPost
...@@ -10,7 +9,7 @@ def create_table(cur): ...@@ -10,7 +9,7 @@ def create_table(cur):
cursor_db = cur cursor_db = cur
cursor_db.execute(QueriesPost().create_table()) cursor_db.execute(QueriesPost().create_table())
except Exception as e: except Exception as e:
print(f'{exception_msg},{e}') print(f'{ConstantsValues().exception_msg},{e}')
return False return False
else: else:
return cursor_db return cursor_db
# updating the db # updating the db
from scripts.constants.proj_constants import ConstantsValues
from scripts.core.database.mongo.queries.mongodb_queires import MongoDBQuery from scripts.core.database.mongo.queries.mongodb_queires import MongoDBQuery
# user data # user data
def input_update_db(): def input_update_db():
columns_ = ['product_id', 'product_name', 'product_category', 'retail_price', columns_ = ConstantsValues().columns_names
'discount_price', 'description', 'overall_rating']
integer_val = [0, 3, 4, 6] integer_val = [0, 3, 4, 6]
counter = 0 counter = 0
condition_values = {} condition_values = {}
......
...@@ -2,13 +2,13 @@ ...@@ -2,13 +2,13 @@
# display conditions # display conditions
import pandas as pd import pandas as pd
from scripts.constants.proj_constants import ConstantsValues
from scripts.core.database.mongo.queries.mongodb_queires import MongoDBQuery from scripts.core.database.mongo.queries.mongodb_queires import MongoDBQuery
# display conditions # display conditions
def view_cond(): def view_cond():
columns_ = ['product_id', 'product_name', 'product_category', 'retail_price', columns_ = ConstantsValues().columns_names
'discount_price', 'description', 'overall_rating']
integer_val = [0, 3, 4, 6] integer_val = [0, 3, 4, 6]
counter = 0 counter = 0
condition_values = {} condition_values = {}
...@@ -30,7 +30,7 @@ def view_fields(db, field_filter): ...@@ -30,7 +30,7 @@ def view_fields(db, field_filter):
view_cursor_count = db.count_documents(field_filter) view_cursor_count = db.count_documents(field_filter)
if view_cursor_count > 0: if view_cursor_count > 0:
print("Data collected....") print("Data collected....")
view_data = pd.json_normalize((view_data_record for view_data_record in view_data_cursor)) view_data = pd.json_normalize([view_data_record for view_data_record in view_data_cursor])
print(view_data) print(view_data)
else: else:
print("Data fetching failed") print("Data fetching failed")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment