-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunction.py
executable file
·89 lines (71 loc) · 2.51 KB
/
function.py
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
import json
import sqlite3
def load_apis_json(json_file):
with open(json_file, 'r') as f:
data = json.load(f)
return data
def create_database(db_name):
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
# Create 'functions' table
cursor.execute('''
CREATE TABLE IF NOT EXISTS functions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
num_params INTEGER NOT NULL
)
''')
# Create 'parameters' table
cursor.execute('''
CREATE TABLE IF NOT EXISTS parameters (
id INTEGER PRIMARY KEY AUTOINCREMENT,
function_id INTEGER NOT NULL,
name TEXT NOT NULL,
type TEXT,
is_optional INTEGER NOT NULL,
default_value TEXT,
FOREIGN KEY (function_id) REFERENCES functions (id)
)
''')
conn.commit()
return conn
def insert_data(conn, data):
cursor = conn.cursor()
for function in data:
function_name = function.get('name')
num_params = function.get('num_params', 0)
params = function.get('params', [])
# Insert function into 'functions' table
cursor.execute('''
INSERT INTO functions (name, num_params)
VALUES (?, ?)
''', (function_name, num_params))
function_id = cursor.lastrowid
# Insert parameters into 'parameters' table
for param in params:
param_name = param.get('name')
param_type = param.get('type')
is_optional = 1 if param.get('is_optional') else 0
default_value = param.get('default_value')
# Convert default_value to string for storage
if default_value is not None:
default_value = str(default_value)
cursor.execute('''
INSERT INTO parameters (function_id, name, type, is_optional, default_value)
VALUES (?, ?, ?, ?, ?)
''', (function_id, param_name, param_type, is_optional, default_value))
conn.commit()
def main():
json_file = 'apis.json' # Path to your JSON file
db_name = 'apis.db' # Name of the SQLite database file
# Load data from JSON file
data = load_apis_json(json_file)
# Create database and tables
conn = create_database(db_name)
# Insert data into database
insert_data(conn, data)
# Close the connection
conn.close()
print(f"Data has been successfully imported into '{db_name}'.")
if __name__ == '__main__':
main()