feat[data]: .data to sql.

This commit is contained in:
damien DELPY 2024-11-13 21:56:41 +01:00
parent 45304ff0b2
commit 4675bfb1fb
No known key found for this signature in database

View File

@ -1,6 +1,26 @@
###############################################################################
#
# PYTHON SCRIPT TO RANDOMLY GENERATE DATA
# STEPS :
# 1 --> Generate a line of a table.
# 2 --> Generate an entire table.
# 3 --> Resolve the issues of dependencies and others.
# 4 --> Convert the created tables to the PostgreSQL code.
# Bonus --> Enjoy !
#
###############################################################################
import random import random
import csv import csv
###############################################################################
#
# HERE IS ALL THE RAW DATA
#
# It is just csv files with for each a single column.
#
###############################################################################
pathIntegers : str = "data/row_numbers.csv" pathIntegers : str = "data/row_numbers.csv"
pathSentences : str = "data/sentences.csv" pathSentences : str = "data/sentences.csv"
pathUrls : str = "data/urls.csv" pathUrls : str = "data/urls.csv"
@ -11,17 +31,25 @@ pathDates : str = "data/dates.csv"
pathColors : str = "data/colors.csv" pathColors : str = "data/colors.csv"
pathBrands : str = "data/brands.csv" pathBrands : str = "data/brands.csv"
###############################################################################
#
# STEP 1 : GENERATE A LINE OF A TABLE
#
###############################################################################
def random_element(pathFile : str) -> str: def random_element(pathFile : str) -> str:
""" """
:param pathFile: the relative path of the csv file to read. :param pathFile: the relative path of the csv file to read.
:return: a random element from this file. :return: a random element from this file.
""" """
with open(pathFile) as file: with open(pathFile, 'r') as file:
csvList : list = list(csv.reader(file)) csvList : list = list(csv.reader(file))
random_index_line : int = random.randint(0, len(csvList)) random_index_line : int = random.randint(0, len(csvList))
return "" if ( csvList[random_index_line] == [] ) \ return "" if ( csvList[random_index_line] == [] ) \
else csvList[random_index_line][0] else csvList[random_index_line][0]
###############################################################################
def construct_line(*args : tuple) -> str: def construct_line(*args : tuple) -> str:
""" """
:param *args: a tuple of elements. :param *args: a tuple of elements.
@ -31,6 +59,8 @@ def construct_line(*args : tuple) -> str:
""" """
return "(" + ', '.join(list(args)) + ")" return "(" + ', '.join(list(args)) + ")"
###############################################################################
def generate_line_acheter() -> str: def generate_line_acheter() -> str:
idMembre : str = random_element(pathIntegers) idMembre : str = random_element(pathIntegers)
idPiece : str = random_element(pathIntegers) idPiece : str = random_element(pathIntegers)
@ -185,9 +215,105 @@ def generate_line_varier() -> str:
return construct_line(idModele_1, idModele_2) return construct_line(idModele_1, idModele_2)
###############################################################################
#
# STEP 2 : GENERATE AN ENTIRE TABLE
#
# It is temporarily stored in a file with the extension .data
#
###############################################################################
def store_table(nbLines : int, pathFile : str, funcGenerationLine) -> None:
"""
:param nbLines: the number of generated lines we want to get.
:param pathFile: the relative path where we want to store the table.
:param funcGenerationLine: the function generate_line_* for the table.
Write the table in a .data file.
"""
with open(pathFile, 'w+') as file: # w+ : if does not exist.
for i in range(nbLines):
file.write(funcGenerationLine() + ",\n")
###############################################################################
#
# STEP 3 : RESOLVE THE ISSUES OF DEPENDENCIES AND OTHERS
#
# Just edits the .data files.
#
###############################################################################
# TODO
###############################################################################
#
# STEP 4 : CONVERSION TO PostgreSQL
#
###############################################################################
def convert_table_to_sql(pathFile : str, nameTable : str) -> None:
"""
:param pathFile: the relative path to the .data file
which contains the table.
:param nameTable: the name of the table.
Write the code in append mode to the file called `insert.sql`.
"""
fileSql = open("insert.sql", "a") # append mode.
fileSql.write("INSERT INTO " + nameTable + "VALUES\n")
with open(pathFile, "r") as fileData:
fileSql.writeLines(fileData.readLines())
fileSql.write(";\n")
fileSql.write("\n")
fileSql.close()
###############################################################################
dictTables : dict = {
"Acheter" : generate_line_acheter,
"Avoir_motif" : generate_line_avoir_motif,
"Avoir_tag" : generate_line_avoir_tag,
"Boites" : generate_line_boites,
"Colorer" : generate_line_colorer,
"Construire" : generate_line_construire,
"Contenir" : generate_line_contenir,
"Couleurs" : generate_line_couleurs,
"Enregistrer" : generate_line_enregistrer,
"Etre" : generate_line_etre,
"Etre_complexe" : generate_line_etre_complexe,
"Etre_forme" : generate_line_etre_forme,
"Fils" : generate_line_fils,
"Illustrations" : generate_line_illustrations,
"Marques" : generate_line_marques,
"Membres" : generate_line_membres,
"Messages" : generate_line_messages,
"Modeles" : generate_line_modeles,
"Necessiter" : generate_line_necessiter,
"Noter" : generate_line_noter,
"Perdre" : generate_line_perdre,
"Tags" : generate_line_tags,
"Varier" : generate_line_varier
}
###############################################################################
if __name__ == '__main__': if __name__ == '__main__':
print(generate_line_acheter()) fileSql = open("insert.sql", "w+")
print(generate_line_boites())
arrayNameTables : list = dictTables.keys()
fileSql.write("TRUNCATE " + ', '.join(arrayNameTables) + ";\n")
fileSql.write("\n")
fileSql.close()
###############################################################################