File size: 5,621 Bytes
cee9158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8f935c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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()