CoolFace
Apppublic

syaikhipin/SQLToSolidity

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
0likes
app.py118 linesDownload Raw Back to root
1import gradio as gr2import requests3from typing import List4 5 6def convert_sql_to_solidity(sql_commands: List[str]) -> str:7    table_name = ""8    solidity_code = "pragma solidity ^0.4.25;\n\n"9    solidity_code += "// import \"./Table.sol\";\n\n"10 11    for sql_command in sql_commands:12        if sql_command.startswith("CREATE TABLE"):13            table_name = sql_command.split()[2]14            columns = sql_command.split("(")[1].split(")")[0].split(", ")15            primary_key = columns[0].split()[0]16            other_columns = [column.split()[0] for column in columns[1:]]17 18            solidity_code += f"contract {table_name.capitalize()} {{\n\n"19            solidity_code += "\taddress private _owner;\n\n"20            solidity_code += "\tmodifier onlyOwner {\n"21            solidity_code += "\t\trequire(_owner == msg.sender, \"Auth: only owner is authorized\");\n"22            solidity_code += "\t\t_;\n"23            solidity_code += "\t}\n\n"24            solidity_code += "\tconstructor() public {\n"25            solidity_code += "\t\t_owner = msg.sender;\n"26            solidity_code += "\t}\n\n"27 28            solidity_code += f"\t// {sql_command}\n"29            solidity_code += f"\tfunction create() public onlyOwner returns (int) {{\n"30            solidity_code += f"\t\tTableFactory tf = TableFactory(0x1001);\n"31            solidity_code += f"\t\tint count = tf.createTable(\"{table_name}\", \"{primary_key}\", \"{', '.join(other_columns)}\");\n"32            solidity_code += f"\t\treturn count;\n"33            solidity_code += f"\t}}\n\n"34 35        elif sql_command.startswith("INSERT INTO"):36            table_name = sql_command.split()[2]37            columns = sql_command.split("(")[1].split(")")[0].split(", ")38            values = sql_command.split("VALUES (")[1].split(")")[0].split(", ")39 40            solidity_code += f"\t// {sql_command}\n"41            solidity_code += f"\tfunction insert({', '.join([f'string {column}' for column in columns])}) public onlyOwner returns (int) {{\n"42            solidity_code += f"\t\tTableFactory tf = TableFactory(0x1001);\n"43            solidity_code += f"\t\tTable table = tf.openTable(\"{table_name}\");\n\n"44            solidity_code += f"\t\tEntry entry = table.newEntry();\n"45 46            for column, value in zip(columns, values):47                column_name = column.split()[0]48                value = value.strip("'")49                solidity_code += f"\t\tentry.set(\"{column_name}\", \"{value}\");\n"50 51            solidity_code += f"\n\t\tint count = table.insert(entry);\n"52            solidity_code += f"\t\treturn count;\n"53            solidity_code += f"\t}}\n\n"54 55        elif sql_command.startswith("UPDATE"):56            table_name = sql_command.split()[1]57            set_column, set_value = sql_command.split("SET ")[1].split(" WHERE ")[0].split(" = ")58            where_column, where_value = sql_command.split(" WHERE ")[1].split(" = ")59 60            set_value = set_value.strip("'")61            where_value = where_value.strip("'")62 63            solidity_code += f"\t// {sql_command}\n"64            solidity_code += f"\tfunction update(string {where_column}, string {set_column}) public onlyOwner returns (int) {{\n"65            solidity_code += f"\t\tTableFactory tf = TableFactory(0x1001);\n"66            solidity_code += f"\t\tTable table = tf.openTable(\"{table_name}\");\n\n"67            solidity_code += f"\t\tEntry entry = table.newEntry();\n"68            solidity_code += f"\t\tentry.set(\"{set_column}\", {set_column});\n\n"69            solidity_code += f"\t\tCondition condition = table.newCondition();\n"70            solidity_code += f"\t\tcondition.EQ(\"{where_column}\", {where_column});\n\n"71            solidity_code += f"\t\tint count = table.update(entry, condition);\n"72            solidity_code += f"\t\treturn count;\n"73            solidity_code += f"\t}}\n\n"74 75        elif sql_command.startswith("DELETE FROM"):76            table_name = sql_command.split()[2]77            where_column, where_value = sql_command.split(" WHERE ")[1].split(" = ")78            where_value = where_value.strip("'")79 80            solidity_code += f"\t// {sql_command}\n"81            solidity_code += f"\tfunction remove(string {where_column}) public onlyOwner returns (int) {{\n"82            solidity_code += f"\t\tTableFactory tf = TableFactory(0x1001);\n"83            solidity_code += f"\t\tTable table = tf.openTable(\"{table_name}\");\n\n"84            solidity_code += f"\t\tCondition condition = table.newCondition();\n"85            solidity_code += f"\t\tcondition.EQ(\"{where_column}\", {where_column});\n\n"86            solidity_code += f"\t\tint count = table.remove(condition);\n"87            solidity_code += f"\t\treturn count;\n"88            solidity_code += f"\t}}\n\n"89 90    solidity_code += "}\n"91    return solidity_code92 93 94def convert_sql_commands(sql: str):95    sql_commands = sql.strip().split(";")96    solidity_code = convert_sql_to_solidity(sql_commands)97    return solidity_code98 99 100iface = gr.Interface(101    fn=convert_sql_commands,102    inputs=gr.inputs.Textbox(lines=5, label="SQL Commands"),103    outputs="text",104    title="SQL to Solidity Converter",105    description="Convert SQL commands into Solidity code for CRUD operations. Table sol URL :https://gist.github.com/syaikhipin/41faf4857f8615645db8c1ebfaf36755",106    examples=[107        ["CREATE TABLE students (id TEXT PRIMARY KEY, name TEXT, age INTEGER);"108         "INSERT INTO students (id, name, age) VALUES ('1', 'Alice', 20);"109         "UPDATE students SET age = 22 WHERE id = '1';"110         "DELETE FROM students WHERE id = '2'"],111    ],112)113 114 115 116 117iface.launch()118