Skip to content

Commit

Permalink
feat: add gen_sql.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Yukaii committed Jul 29, 2023
1 parent 1c1103c commit 6d85ef0
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions session-data/gen_sql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import json

# Function to generate INSERT statement for a record
def generate_insert_sql(table, record):
quoted_keys = ['"start"', '"end"']

if table == 'sessions':
keys = ['id', 'type', 'room', '"start"', '"end"', 'language', 'co_write', 'uri']
values = [record.get(key) for key in keys]
zh_keys = ['title', 'description']
keys += zh_keys
values += [record['zh'].get(key) for key in zh_keys]
elif table in ['speakers', 'session_types', 'rooms', 'tags']:
keys = ['id']
values = [record.get(key) for key in keys]
zh_keys = ['name', 'description']
keys += zh_keys
values += [record['zh'].get(key) for key in zh_keys]

# Format values into SQL-friendly string
values = ["NULL" if val is None else "'{}'".format(str(val).replace("'", "''")) for val in values]

return f"INSERT INTO {table} ({', '.join(keys)}) VALUES ({', '.join(values)});\n"

# Load the JSON file
with open('session.json', 'r') as f:
data = json.load(f)

# Prepare the SQL output file
with open('output.sql', 'w') as f:
# Generate SQL INSERT statements for each table
for table_name in data.keys():
for record in data[table_name]:
f.write(generate_insert_sql(table_name, record))

0 comments on commit 6d85ef0

Please sign in to comment.