• XSS.stack #1 – первый литературный журнал от юзеров форума

Статья (Open-Source) Implementation in Julia for parsing a big .sql files and splitting it into a .csv (OOP)

root3d

RAID-массив
Забанен
Регистрация
24.11.2022
Сообщения
69
Реакции
29
Пожалуйста, обратите внимание, что пользователь заблокирован
There are 2 ways of implementation to choose from.
Based from user input.
The program implements object-oriented programming concepts and takes advantage of Julia's built-in functionality for reading and writing to files.

Method 1: (Without error handling)
source:
Код:
# Define a SqlParser type to store information about a .sql file
struct SqlParser
    file_path::String
end

# Define a method for the SqlParser type to split the .sql file into a .csv format
function split_to_csv(parser::SqlParser, columns::Vector{String})
    # Open the .sql file for reading
    file = open(parser.file_path, "r")
    
    # Read the entire contents of the .sql file into a string
    sql_data = read(file, String)
    
    # Close the file to free up resources
    close(file)
    
    # Split the contents of the .sql file into rows
    rows = split(sql_data, ";\n")
    
    # Open a .csv file for writing
    csv_file = open("output.csv", "w")
    
    # Write the header row to the .csv file
    write(csv_file, join(columns, ","), "\n")
    
    # Loop through each row in the .sql file
    for row in rows
        # Split the row into columns
        values = split(row, ",")
        
        # Store the values for the specified columns in a dictionary
        data = Dict{String, String}()
        for (i, column) in enumerate(columns)
            data[column] = values[i]
        end
        
        # Write the values for the specified columns to the .csv file
        write(csv_file, join([data[column] for column in columns], ","), "\n")
    end
    
    # Close the .csv file to save the changes
    close(csv_file)
end

# Prompt the user for the .sql file path
println("Enter the path to the .sql file:")
file_path = readline()

# Prompt the user for the columns to include in the .csv file
println("Enter the columns to include in the .csv file (separated by commas):")
columns = split(readline(), ",")

# Create a SqlParser instance
parser = SqlParser(file_path)

# Call the split_to_csv method to split the .sql file into a .csv format
split_to_csv(parser, columns)

# Print a message indicating that the .csv file has been created
println("The .csv file has been created!")

Method 2: (With error handling and user invalid input)

source:
Код:
# Importing required libraries
using DataFrames, CSV

# Defining the SQLFileParser struct
struct SQLFileParser
    filepath::String
    
    # The constructor function
    function SQLFileParser(filepath::String)
        if !isfile(filepath)
            error("File not found: $filepath")
        end
        new(filepath)
    end
    
    # Method for parsing the SQL file
    function parse(self)
        try
            # Reading the SQL file into a string
            sql_text = read(self.filepath, String)
            
            # Splitting the SQL text into separate statements
            statements = split(sql_text, ";")
            
            # Parsing each statement and storing the result in a data frame
            df = DataFrame(Statement = statements)
            
            return df
        catch e
            error("Error while reading file: $e")
        end
    end
    
    # Method for printing the parsed data to a CSV file
    function print_to_csv(self, csv_filepath::String)
        try
            df = self.parse()
            csv.write(csv_filepath, df)
            println("Data written to file: $csv_filepath")
        catch e
            error("Error while writing to CSV file: $e")
        end
    end
end

# Testing the SQLFileParser struct
try
    parser = SQLFileParser("sample.sql")
    parser.print_to_csv("sample.csv")
catch e
    println("Error: $e")
end


Pass: local

 

Вложения

  • SQLParser_Julia.zip
    1.8 КБ · Просмотры: 14


Напишите ответ...
  • Вставить:
Прикрепить файлы
Верх