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 @@
import psycopg2 as py
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:
postgres_client = py.connect(host=host_name, port=port, user=user, password=password)
......@@ -12,7 +12,7 @@ try:
# creating cursor for the postgres
cur = postgres_client.cursor()
cur.execute("SELECT datname FROM pg_database WHERE datistemplate = false;")
cur.execute(QueriesPost().databases())
# Fetch the result
result = cur.fetchall()
......@@ -27,4 +27,4 @@ try:
postgres_client.commit()
cur.execute(f'CREATE DATABASE {dbname}')
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
class QueriesPost:
@staticmethod
def databases():
db = "SELECT datname FROM pg_database WHERE datistemplate = false;"
return db
@staticmethod
def create_table():
table = f"CREATE TABLE if not exists {products} " \
f"(Product_Id SERIAL PRIMARY KEY, Product_Name VARCHAR(255)," \
f"Product_Category VARCHAR(255)," \
f"Retail_Price INT," \
f"Discounted_Price INT," \
f"Description VARCHAR(255)," \
f"Overall_Rating INT)"
f"(product_id SERIAL PRIMARY KEY, product_name VARCHAR(255)," \
f"product_category VARCHAR(255)," \
f"retail_price INT," \
f"discounted_price INT," \
f"description VARCHAR(255)," \
f"overall_rating INT)"
return table
@staticmethod
def insert_data():
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)"
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)"
return insert_data
@staticmethod
......@@ -31,10 +36,10 @@ class QueriesPost:
@staticmethod
def fetch_all():
fetch_data = f"SELECT * FROM {products}"
fetch_data = f"SELECT * FROM {products} ORDER BY product_id ASC"
return fetch_data
@staticmethod
def delete_data(field_name, del_data):
del_val = f"DELETE FROM {products} WHERE {field_name} = {del_data}"
def delete_data(del_data):
del_val = f"DELETE FROM {products} WHERE {del_data}"
return del_val
......@@ -9,7 +9,7 @@ def read_inputs():
retail_price = int(input("Enter the retail price: "))
discount_price = int(input("Enter the discounted price: "))
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
......@@ -17,8 +17,8 @@ def read_inputs():
# inserting data to mongo
def insert_data_db(db, p_id, p_name, p_category, retail_price, discount_price, description, over_rating):
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
except Exception as e:
print("Exception occurred :", e)
# deleting data
# getting user inputs
from scripts.constants.proj_constants import ConstantsValues
from scripts.core.database.mongo.queries.mongodb_queires import MongoDBQuery
def delete_db_input():
columns_ = ['product_id', 'product_name', 'product_category', 'retail_price',
'discount_price', 'description', 'overall_rating']
columns_ = ConstantsValues().columns_names
integer_val = [0, 3, 4, 6]
counter = 0
condition_values = {}
......
from scripts.config.postgres_connect_config import products
from scripts.core.database.postgres.postgres_connect import cur
from scripts.core.database.postgres.queries.post_queries import QueriesPost
def post_columns():
# 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
field_names = [desc[0] for desc in cur.description]
......
# 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.handlers.data_insert import insert_data_db, read_inputs
from scripts.core.handlers.delete_db_data import delete_db_input, db_delete
......@@ -16,8 +17,8 @@ class MongoDbStart:
except Exception as e:
print("Connection failed: ", e)
else:
db = client_con['storeManager']
store_manager = db['products']
db = client_con[dbname]
store_manager = db[products]
return store_manager
# mongo data insert
......@@ -49,7 +50,7 @@ class MongoDbStart:
else:
print("Data not updated")
except Exception as e:
print(f'{exception_msg}{e}')
print(f'{ConstantsValues().exception_msg}{e}')
# mongo delete data
@staticmethod
......@@ -62,7 +63,7 @@ class MongoDbStart:
else:
print("Data not deleted")
except Exception as e:
print(f'{exception_msg}{e}')
print(f'{ConstantsValues().exception_msg}{e}')
# mongo view data
@staticmethod
......@@ -72,6 +73,4 @@ class MongoDbStart:
view_fields(db, field_filter)
except Exception as e:
print(f'{exception_msg}{e}')
print(f'{ConstantsValues().exception_msg}{e}')
# deleting data
# 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
# user input
def delete_db_post():
field_name = input("Enter the field condition: ")
del_data = int(input("Enter the data: "))
columns_ = ConstantsValues().columns_names
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
def db_delete_post(cur, field_name, del_data):
return cur.execute(QueriesPost().delete_data(field_name, del_data))
def db_delete_post(cur, condition_values):
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
from scripts.config.postgres_connect_config import products
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
# user inputs
def read_inputs_post():
p_id = int(input("Enter the product id: "))
p_name = input("Enter the product name: ")
p_category = input("Enter the category: ")
retail_price = int(input("Enter the retail price: "))
......@@ -14,7 +12,7 @@ def read_inputs_post():
description = input("Enter the product description: ")
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
......@@ -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]
cur.execute(QueriesPost().insert_data(), val_data)
except Exception as e:
print(f'{exception_msg},{e}')
print(f'{ConstantsValues().exception_msg},{e}')
return False
else:
return True
# 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.handlers.get_post_columns import post_columns
# user data
def input_update_post():
columns_ = post_columns()
columns_ = ConstantsValues().columns_names
counter = 0
condition_values = {}
print("Conditions")
......@@ -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 = ", ".join(updates)
if conditions and updates:
cur = cur.execute(QueriesPost().update_data())
cur = cur.execute(QueriesPost().update_data(updates, conditions))
else:
cur = None
return cur
......@@ -2,7 +2,7 @@
# display conditions
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.handlers.get_post_columns import post_columns
......@@ -27,17 +27,13 @@ def view_fields_post(cur, condition_values):
conditions = [f"{field} = '{value}'" for field, value in condition_values.items()]
conditions = " and ".join(conditions)
if conditions:
cur.execute(QueriesPost().fetch_conditions())
print(conditions)
cur.execute(QueriesPost().fetch_conditions(conditions))
cursor_data = cur.fetchall()
if cursor_data:
print("Data collected....")
view_data = pd.DataFrame((view_data_record for view_data_record in cursor_data), columns=['Product Id',
'Product_Name',
'Product_Category',
'Retail_Price',
'Discounted_Price',
'Description',
'Overall_Rating'])
view_data = pd.DataFrame((view_data_record for view_data_record in cursor_data),
columns=ConstantsValues().columns_names)
print(view_data)
else:
print("Data fetching failed")
......@@ -46,13 +42,8 @@ def view_fields_post(cur, condition_values):
cursor_data = cur.fetchall()
if cursor_data:
print("Data collected....")
view_data = pd.DataFrame((view_data_record for view_data_record in cursor_data), columns=['Product Id',
'Product_Name',
'Product_Category',
'Retail_Price',
'Discounted_Price',
'Description',
'Overall_Rating'])
view_data = pd.DataFrame((view_data_record for view_data_record in cursor_data),
columns=ConstantsValues().columns_names)
print(view_data)
else:
print("Data fetching failed")
# 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.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
......@@ -19,14 +19,14 @@ class PostgresStart:
else:
return check_result
except Exception as e:
print(f'{exception_msg}{e}')
print(f'{ConstantsValues().exception_msg}{e}')
@staticmethod
def post_insert():
try:
num_prod = int(input("Enter the number of products: "))
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,
p_category, retail_price, discount_price, description, over_rating)
if insert_check:
......@@ -47,19 +47,19 @@ class PostgresStart:
else:
print("Data not updated")
except Exception as e:
print(f'{exception_msg},{e}')
print(f'{ConstantsValues().exception_msg},{e}')
@staticmethod
def post_delete():
try:
field_name, del_data = delete_db_post()
del_check = db_delete_post(cur, field_name, del_data)
condition_values = delete_db_post()
del_check = db_delete_post(cur, condition_values)
if del_check is None:
print("Data deleted")
else:
print("Data not deleted")
except Exception as e:
print(f'{exception_msg}{e}')
print(f'{ConstantsValues().exception_msg}{e}')
@staticmethod
def post_view():
......@@ -68,11 +68,10 @@ class PostgresStart:
view_fields_post(cur, condition_values)
except Exception as e:
print(f'{exception_msg}{e}')
print(f'{ConstantsValues().exception_msg}{e}')
@staticmethod
def post_quit():
# Close the cursor and connection
cur.close()
postgres_client.close()
# creating a table
from scripts.config.postgres_connect_config import products
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
......@@ -10,7 +9,7 @@ def create_table(cur):
cursor_db = cur
cursor_db.execute(QueriesPost().create_table())
except Exception as e:
print(f'{exception_msg},{e}')
print(f'{ConstantsValues().exception_msg},{e}')
return False
else:
return cursor_db
# updating the db
from scripts.constants.proj_constants import ConstantsValues
from scripts.core.database.mongo.queries.mongodb_queires import MongoDBQuery
# user data
def input_update_db():
columns_ = ['product_id', 'product_name', 'product_category', 'retail_price',
'discount_price', 'description', 'overall_rating']
columns_ = ConstantsValues().columns_names
integer_val = [0, 3, 4, 6]
counter = 0
condition_values = {}
......
......@@ -2,13 +2,13 @@
# display conditions
import pandas as pd
from scripts.constants.proj_constants import ConstantsValues
from scripts.core.database.mongo.queries.mongodb_queires import MongoDBQuery
# display conditions
def view_cond():
columns_ = ['product_id', 'product_name', 'product_category', 'retail_price',
'discount_price', 'description', 'overall_rating']
columns_ = ConstantsValues().columns_names
integer_val = [0, 3, 4, 6]
counter = 0
condition_values = {}
......@@ -30,7 +30,7 @@ def view_fields(db, field_filter):
view_cursor_count = db.count_documents(field_filter)
if view_cursor_count > 0:
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)
else:
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