Upload sqlite_parser.py
Browse filesscript for creating your own db_schemas.json from your own sql files.
- sqlite_parser.py +49 -0
sqlite_parser.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
from typing import Union, List, Dict
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from itertools import chain
|
| 7 |
+
from simple_ddl_parser import parse_from_file
|
| 8 |
+
|
| 9 |
+
class DBParser:
|
| 10 |
+
def __init__(self, db_path:Union[str, Path]) -> None:
|
| 11 |
+
self.db_path = db_path
|
| 12 |
+
self.suffix:str =".sql"
|
| 13 |
+
self.primary_key_token:str = "primary key:"
|
| 14 |
+
self.foreign_key_token:str = "foreign_key:"
|
| 15 |
+
self.separator:str = " [SEP] "
|
| 16 |
+
|
| 17 |
+
@staticmethod
|
| 18 |
+
def dump_sqlite_to_sql(path_to_sqlite: Union[str, Path], output_path: Union[str, Path]) -> None:
|
| 19 |
+
assert path_to_sqlite.endswith('.sqlite')
|
| 20 |
+
con = sqlite3.connect(path_to_sqlite)
|
| 21 |
+
with open(output_path, 'w') as f:
|
| 22 |
+
for line in con.iterdump():
|
| 23 |
+
f.write('%s\n' % line)
|
| 24 |
+
|
| 25 |
+
def parse_table(self, table_schema:dict) -> str:
|
| 26 |
+
normal_keys = " ".join(list(chain.from_iterable((column["name"], column["type"], ",") for column in table_schema["columns"] if column["references"] is None)))
|
| 27 |
+
foreign_keys =" ".join(list(chain.from_iterable((column["name"], column["type"],"from", column["references"]["table"], column["references"]["column"], ",") for column in table_schema["columns"] if column["references"] is not None)))
|
| 28 |
+
primary_keys = " ".join(table_schema["primary_key"])
|
| 29 |
+
return " ".join([table_schema["table_name"], normal_keys, self.foreign_key_token, foreign_keys, self.primary_key_token, primary_keys])
|
| 30 |
+
|
| 31 |
+
def parse_schema(self, schema:List[dict]) -> str:
|
| 32 |
+
table_schemas: List[str] = [self.parse_table(table) for table in schema if 'columns' in table]
|
| 33 |
+
return self.separator.join(table_schemas)
|
| 34 |
+
|
| 35 |
+
def create_db_prompt_dict(self, output_file: str = 'db_schemas.json') -> Dict[str, str]:
|
| 36 |
+
db_schema_dict = {}
|
| 37 |
+
for dir in os.listdir(self.db_path):
|
| 38 |
+
print("Processing database: ", dir)
|
| 39 |
+
filenames = [i for i in os.listdir(Path(self.db_path, dir)) if i.endswith(self.suffix)]
|
| 40 |
+
path_to_db = Path(self.db_path, dir,filenames[0])
|
| 41 |
+
schema = parse_from_file(path_to_db)
|
| 42 |
+
db_schema_dict[dir]=self.parse_schema(schema)
|
| 43 |
+
with open(output_file, 'w') as f:
|
| 44 |
+
f.write(json.dumps(db_schema_dict))
|
| 45 |
+
return db_schema_dict
|
| 46 |
+
|
| 47 |
+
# # Usage
|
| 48 |
+
# db_parser = DBParser(<PATH_To_DATABASES>)
|
| 49 |
+
# db_parser.create_db_prompt_dict()
|