Skip to content

Commit

Permalink
insert into WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
coilysiren committed Oct 21, 2023
1 parent ee9db61 commit ee07536
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 18 deletions.
21 changes: 21 additions & 0 deletions data/sql_input_2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- https://cratedb.com/docs/sql-99/en/latest/chapters/01.html
-- https://www.postgresql.org/docs/16/sql-createtable.html
-- https://www.postgresql.org/docs/16/sql-insert.html
-- https://www.postgresql.org/docs/16/sql-select.html
CREATE TABLE city (
name VARCHAR,
population INT,
timezone INT
);

INSERT INTO city (name, population, timezone)
VALUES ('San Francisco', 852469, -8);

INSERT INTO city (name, population, timezone)
VALUES ('New York', 8405837, -5);

SELECT
name,
population,
timezone
FROM city;
5 changes: 5 additions & 0 deletions data/sql_output_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": ["San Francisco", "New York"],
"population": [852469, 8405837],
"timezone": [-8, -5]
}
81 changes: 63 additions & 18 deletions src/python/sql_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,61 @@


class SQL:
data: dict = {}
__data: dict = {}

def __init__(self) -> None:
self.data = {}
self.clear_data()

def information_schema_tables(self) -> list[dict]:
return [data["metadata"] for data in self.data.values()]
def clear_data(self):
self.__data = {}

def read_data_table(self, table_name: str) -> dict:
return self.__data.get(table_name, {})

def read_information_schema_tables(self) -> list[dict]:
return [data["metadata"] for data in self.__data.values()]

def write_table_meta(self, table_name: str, data: dict):
self.__data[table_name] = data

def write_table_data(self, table_name: str, data: dict):
self.__data[table_name]["data"] = data

def create_table(self, *args, table_schema="public") -> dict:
table_name = args[2]
if not self.data.get(table_name):
self.data[table_name] = {
"metadata": {
"table_name": table_name,
"table_schema": table_schema,
},

# get columns
columns = {}
columns_str = " ".join(args[3:]).replace("(", "").replace(")", "").strip()
if columns_str:
# fmt: off
columns = {
column.strip().split(" ")[0]: column.strip().split(" ")[1]
for column in columns_str.split(",")
}
# fmt: on

if self.read_data_table(table_name):
self.write_table_meta(
table_name,
{
"metadata": {
"table_name": table_name,
"table_schema": table_schema,
"colums": columns,
}
},
)
return {}

create_table.sql = "CREATE TABLE"

def insert_into(self, *args) -> dict:
print(f"args: {args}")
pass

insert_into.sql = "INSERT INTO"

def select(self, *args) -> dict:
output = {}

Expand All @@ -54,7 +88,9 @@ def select(self, *args) -> dict:
# consider "information_schema.tables" a special case until
# we figure out why its so different from the others
if from_value == "information_schema.tables":
target = self.information_schema_tables()
target = self.read_information_schema_tables()
else:
target = self.read_data_table(from_value)

# fmt: off
output = {
Expand All @@ -74,19 +110,28 @@ def select(self, *args) -> dict:
sql_map = {
create_table.sql: create_table,
select.sql: select,
insert_into.sql: insert_into,
}

def run(self, input_sql: list[str]) -> list[str]:
output = {}

# remove comments
input_sql = [line.strip() for line in input_sql if not line.startswith("--")]

# re-split on semi-colons
input_sql = " ".join(input_sql).split(";")

# iterate over each line of sql
for line in input_sql:
if not line.startswith("--"):
words = line.split(" ")
for i in reversed(range(len(words))):
key = " ".join(words[:i])
if func := self.sql_map.get(key):
output = func(self, *words)
break
words = line.split(" ")
for i in reversed(range(len(words) + 1)):
key = " ".join(words[:i]).strip()
# print(f'key: "{key}"')
if func := self.sql_map.get(key):
# print(f'running "{func.__name__}" with {words}')
output = func(self, *[word for word in words if word])
break

return [json.dumps(output)]

Expand Down

0 comments on commit ee07536

Please sign in to comment.