Spaces:
Sleeping
Sleeping
import gradio as gr | |
import requests | |
from typing import List | |
def convert_sql_to_solidity(sql_commands: List[str]) -> str: | |
table_name = "" | |
solidity_code = "pragma solidity ^0.4.25;\n\n" | |
solidity_code += "// import \"./Table.sol\";\n\n" | |
for sql_command in sql_commands: | |
if sql_command.startswith("CREATE TABLE"): | |
table_name = sql_command.split()[2] | |
columns = sql_command.split("(")[1].split(")")[0].split(", ") | |
primary_key = columns[0].split()[0] | |
other_columns = [column.split()[0] for column in columns[1:]] | |
solidity_code += f"contract {table_name.capitalize()} {{\n\n" | |
solidity_code += "\taddress private _owner;\n\n" | |
solidity_code += "\tmodifier onlyOwner {\n" | |
solidity_code += "\t\trequire(_owner == msg.sender, \"Auth: only owner is authorized\");\n" | |
solidity_code += "\t\t_;\n" | |
solidity_code += "\t}\n\n" | |
solidity_code += "\tconstructor() public {\n" | |
solidity_code += "\t\t_owner = msg.sender;\n" | |
solidity_code += "\t}\n\n" | |
solidity_code += f"\t// {sql_command}\n" | |
solidity_code += f"\tfunction create() public onlyOwner returns (int) {{\n" | |
solidity_code += f"\t\tTableFactory tf = TableFactory(0x1001);\n" | |
solidity_code += f"\t\tint count = tf.createTable(\"{table_name}\", \"{primary_key}\", \"{', '.join(other_columns)}\");\n" | |
solidity_code += f"\t\treturn count;\n" | |
solidity_code += f"\t}}\n\n" | |
elif sql_command.startswith("INSERT INTO"): | |
table_name = sql_command.split()[2] | |
columns = sql_command.split("(")[1].split(")")[0].split(", ") | |
values = sql_command.split("VALUES (")[1].split(")")[0].split(", ") | |
solidity_code += f"\t// {sql_command}\n" | |
solidity_code += f"\tfunction insert({', '.join([f'string {column}' for column in columns])}) public onlyOwner returns (int) {{\n" | |
solidity_code += f"\t\tTableFactory tf = TableFactory(0x1001);\n" | |
solidity_code += f"\t\tTable table = tf.openTable(\"{table_name}\");\n\n" | |
solidity_code += f"\t\tEntry entry = table.newEntry();\n" | |
for column, value in zip(columns, values): | |
column_name = column.split()[0] | |
value = value.strip("'") | |
solidity_code += f"\t\tentry.set(\"{column_name}\", \"{value}\");\n" | |
solidity_code += f"\n\t\tint count = table.insert(entry);\n" | |
solidity_code += f"\t\treturn count;\n" | |
solidity_code += f"\t}}\n\n" | |
elif sql_command.startswith("UPDATE"): | |
table_name = sql_command.split()[1] | |
set_column, set_value = sql_command.split("SET ")[1].split(" WHERE ")[0].split(" = ") | |
where_column, where_value = sql_command.split(" WHERE ")[1].split(" = ") | |
set_value = set_value.strip("'") | |
where_value = where_value.strip("'") | |
solidity_code += f"\t// {sql_command}\n" | |
solidity_code += f"\tfunction update(string {where_column}, string {set_column}) public onlyOwner returns (int) {{\n" | |
solidity_code += f"\t\tTableFactory tf = TableFactory(0x1001);\n" | |
solidity_code += f"\t\tTable table = tf.openTable(\"{table_name}\");\n\n" | |
solidity_code += f"\t\tEntry entry = table.newEntry();\n" | |
solidity_code += f"\t\tentry.set(\"{set_column}\", {set_column});\n\n" | |
solidity_code += f"\t\tCondition condition = table.newCondition();\n" | |
solidity_code += f"\t\tcondition.EQ(\"{where_column}\", {where_column});\n\n" | |
solidity_code += f"\t\tint count = table.update(entry, condition);\n" | |
solidity_code += f"\t\treturn count;\n" | |
solidity_code += f"\t}}\n\n" | |
elif sql_command.startswith("DELETE FROM"): | |
table_name = sql_command.split()[2] | |
where_column, where_value = sql_command.split(" WHERE ")[1].split(" = ") | |
where_value = where_value.strip("'") | |
solidity_code += f"\t// {sql_command}\n" | |
solidity_code += f"\tfunction remove(string {where_column}) public onlyOwner returns (int) {{\n" | |
solidity_code += f"\t\tTableFactory tf = TableFactory(0x1001);\n" | |
solidity_code += f"\t\tTable table = tf.openTable(\"{table_name}\");\n\n" | |
solidity_code += f"\t\tCondition condition = table.newCondition();\n" | |
solidity_code += f"\t\tcondition.EQ(\"{where_column}\", {where_column});\n\n" | |
solidity_code += f"\t\tint count = table.remove(condition);\n" | |
solidity_code += f"\t\treturn count;\n" | |
solidity_code += f"\t}}\n\n" | |
solidity_code += "}\n" | |
return solidity_code | |
def convert_sql_commands(sql: str): | |
sql_commands = sql.strip().split(";") | |
solidity_code = convert_sql_to_solidity(sql_commands) | |
return solidity_code | |
iface = gr.Interface( | |
fn=convert_sql_commands, | |
inputs=gr.inputs.Textbox(lines=5, label="SQL Commands"), | |
outputs="text", | |
title="SQL to Solidity Converter", | |
description="Convert SQL commands into Solidity code for CRUD operations. Table sol URL :https://gist.github.com/syaikhipin/41faf4857f8615645db8c1ebfaf36755", | |
examples=[ | |
["CREATE TABLE students (id TEXT PRIMARY KEY, name TEXT, age INTEGER);" | |
"INSERT INTO students (id, name, age) VALUES ('1', 'Alice', 20);" | |
"UPDATE students SET age = 22 WHERE id = '1';" | |
"DELETE FROM students WHERE id = '2'"], | |
], | |
) | |
iface.launch() | |