-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.sh
188 lines (152 loc) · 4.27 KB
/
database.sh
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/bash
# Function to convert Solidity type to database type
solidity_type_to_db_type() {
case $1 in
uint256|uint)
echo "INTEGER"
;;
address)
echo "STRING"
;;
bytes32)
echo "STRING"
;;
*)
echo "STRING"
;;
esac
}
# Function to parse Solidity struct and generate table definition
parse_solidity_struct() {
local struct_name=$1
local members=$2
local table="{\"name\": \"$struct_name\", \"columns\": ["
local first=true
echo "$members" | jq -c '.[]' | while read -r member; do
local name=$(echo "$member" | jq -r '.name')
local type=$(echo "$member" | jq -r '.typeName.name // .typeName.baseTypeName.name')
local db_type=$(solidity_type_to_db_type "$type")
if $first; then
first=false
else
table+=","
fi
table+="{\"name\": \"$name\", \"type\": \"$db_type\"}"
done
table+="]}"
echo "$table"
}
# Function to parse Solidity contract and generate database definition
parse_solidity_contract() {
local contract_name=$1
local sub_nodes=$2
local database="{\"name\": \"$contract_name\", \"tables\": ["
local first=true
echo "$sub_nodes" | jq -c '.[]' | while read -r node; do
local type=$(echo "$node" | jq -r '.type')
if [ "$type" == "StructDefinition" ]; then
local struct_name=$(echo "$node" | jq -r '.name')
local members=$(echo "$node" | jq -c '.members')
local table=$(parse_solidity_struct "$struct_name" "$members")
if $first; then
first=false
else
database+=","
fi
database+="$table"
elif [ "$type" == "FunctionDefinition" ]; then
local function_name=$(echo "$node" | jq -r '.name')
local columns="[]"
columns=$(echo "$node" | jq -c '[.body.statements[] | select(.type == "VariableDeclarationStatement") | .variables[0] | {name: .name, typeName: .typeName}]')
local table=$(parse_solidity_struct "$function_name" "$columns")
if $first; then
first=false
else
database+=","
fi
database+="$table"
fi
done
database+="]}"
echo "$database"
}
# Function to generate JSON output
generate_json() {
echo "$1" | jq .
}
# Function to generate SQL output
generate_sql() {
local database=$1
local name=$(echo "$database" | jq -r '.name')
local tables=$(echo "$database" | jq -c '.tables[]')
echo "CREATE DATABASE $name;"
echo
echo "$tables" | while read -r table; do
local table_name=$(echo "$table" | jq -r '.name')
local columns=$(echo "$table" | jq -c '.columns[]')
echo "CREATE TABLE $table_name ("
first=true
echo "$columns" | while read -r column; do
local column_name=$(echo "$column" | jq -r '.name')
local column_type=$(echo "$column" | jq -r '.type')
if $first; then
first=false
else
echo ","
fi
echo -n " $column_name $column_type"
done
echo
echo ");"
echo
done
}
# Function to generate NoSQL output
generate_nosql() {
local database=$1
local name=$(echo "$database" | jq -r '.name')
local tables=$(echo "$database" | jq -c '.tables[]')
echo "Database: $name"
echo
echo "$tables" | while read -r table; do
local table_name=$(echo "$table" | jq -r '.name')
local columns=$(echo "$table" | jq -c '.columns[]')
echo "Collection: $table_name"
echo "$columns" | while read -r column; do
local column_name=$(echo "$column" | jq -r '.name')
local column_type=$(echo "$column" | jq -r '.type')
echo " $column_name: $column_type"
done
echo
done
}
# Main script
if [ -z "$1" ]; then
echo "Usage: $0 <Solidity file> [format]"
echo "Formats: json, sql, nosql"
exit 1
fi
SOLIDITY_FILE=$1
FORMAT=${2:-json}
# Generate AST from Solidity file
AST=$(solc --ast-json "$SOLIDITY_FILE")
# Parse the AST and extract the contract definition
CONTRACT=$(echo "$AST" | jq -c '.children[] | select(.type == "ContractDefinition")')
CONTRACT_NAME=$(echo "$CONTRACT" | jq -r '.name')
SUB_NODES=$(echo "$CONTRACT" | jq -c '.subNodes')
DATABASE=$(parse_solidity_contract "$CONTRACT_NAME" "$SUB_NODES")
case $FORMAT in
json)
generate_json "$DATABASE"
;;
sql)
generate_sql "$DATABASE"
;;
nosql)
generate_nosql "$DATABASE"
;;
*)
echo "Invalid format: $FORMAT"
exit 1
;;
esac