Skip to content

sql parser init #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"python.pythonPath": "/Users/shuvayan/.pyenv/versions/3.9.7/bin/python",
"python.formatting.provider": "black"
}
12 changes: 11 additions & 1 deletion gsql/backend/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,21 @@ def save_token(self):
with open(os.path.join(self.store_folder, "token.json"), "w") as token:
token.write(self.creds.to_json())

@staticmethod
def get_creds():
"""
static method to get the credentials
"""
store_folder = os.path.join(os.path.expanduser("~"), ".gsql")
return Credentials.from_authorized_user_file(
os.path.join(store_folder, "token.json"), SCOPES
)

def auth(self):
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists(self.store_folder + "/token.json"):
if os.path.exists(os.path.join(self.store_folder, "token.json")):
self.creds = Credentials.from_authorized_user_file(
os.path.join(self.store_folder, "token.json"), SCOPES
)
Expand Down
104 changes: 104 additions & 0 deletions gsql/backend/sqlite_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import sqlite3
import os
import pandas as pd
from pandas.core.frame import DataFrame
import namegenerator

class SQLiteManager:
'''
a Manager class to help with SQL queries
'''

def __init__(self) -> None:
self.store_location = os.path.join(os.path.expanduser('~'), '.gsql', 'databases')
os.makedirs(self.store_location, exist_ok=True)
self.common_db_path = os.path.join( self.store_location , 'common.db')

def _write_to_common(self, resultset):

# convert the resultset dictionary into a set of tuples
insert_list = []

con = sqlite3.connect(self.common_db_path)
curr = con.cursor()
curr.execute("create table if not exists common (title varchar(50), id varchar(50) primary key, \
nickname varchar(50))")

table = pd.read_sql_query("select * from common", con)
for content in resultset:
if content['id'] not in table['id'].tolist():
insert_list.append((content['title'], content['id'], namegenerator.gen()))



curr.executemany("replace into common values(?, ?, ?);", insert_list)

con.commit()
con.close()

def _read_from_common(self) -> pd.DataFrame:

con = sqlite3.connect(self.common_db_path)
table = pd.read_sql_query("select * from common", con)
con.close()
return table


def read_generic_select_statement(self, table_name, statement ) -> pd.DataFrame:
'''
reads content from the database corresponding to a table name
and wraps it in a dataframe

params
-------
table_name : Name of the table (str)
statement : raw SQL statement to be passed on to SQLite
'''
db_path = os.path.join(self.store_location, table_name + '.db')
con = sqlite3.connect(db_path)
table = pd.read_sql_query(statement, con)
con.close()
return table


def write_metadata(self, metadata) -> None:

# preparing the metadata into tuple
insert_list = []
db_id = metadata["spreadsheetId"]
db_name = metadata["title"]

for sheet in metadata['sheets']:
properties = sheet['properties']
insert_list.append((db_id, db_name, properties['sheetId'], properties['title'],
properties['gridProperties']['rowCount'], properties['gridProperties']['columnCount']))


metadata_path = os.path.join( self.store_location , 'metadata_{}.db'.format(db_id))
con = sqlite3.connect(metadata_path)
curr = con.cursor()
curr.execute("create table if not exists metadata (db_id varchar(50), db_name varchar(50), \
sheet_id varchar(20) primary key, sheet_title varchar(50), row_count integer, col_count integer)")
curr.executemany("replace into metadata values(?, ?, ?, ?, ?, ?)" , insert_list)
con.commit()
con.close()

def read_metadata(self, db_id) -> pd.DataFrame:
'''
reads out the data corresponding to a particular ID
'''

metadata_path = os.path.join( self.store_location , 'metadata_{}.db'.format(db_id))
con = sqlite3.connect(metadata_path)
table = pd.read_sql_query("select * from metadata", con)
con.close()
return table









6 changes: 6 additions & 0 deletions gsql/exceptions/sqlparser_exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


class SQLStatmentException(Exception):
def __init__(self, message="Invalid SQL statement") -> None:
super().__init__(message)
self.message = message
27 changes: 24 additions & 3 deletions gsql/frontend/shell/driver.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from gsql.backend.sqlite_manager import SQLiteManager
from gsql.frontend.constants import Commands
from gsql.logging import logger
from gsql.frontend.shell.shell import GSQLShell
from rich import print
from gsql.backend.auth import Auth
from gsql.console import console
from gsql.backend.api_handler import ApiHandler
import sys


class GSQLDriver:
Expand All @@ -14,11 +17,13 @@ class GSQLDriver:
def __init__(self, action: str) -> None:
self.action = action
logger.debug("GSQL called with action :{}".format(self.action))
self.shell_instance = GSQLShell()
self.shell_instance = None
self.auth = Auth()
self.api = None
self.sqlite_manager = SQLiteManager()

def authenticate(self):
auth = Auth()
err = auth.auth()
err = self.auth.auth()
if err:
logger.error("Authentication failed: {}".format(err))
console.print("[red]Authentication failed!!!")
Expand All @@ -43,6 +48,22 @@ def show_help(self):
print("help")

def start_shell(self):
# check if not authenticated force the user to authenticate
err = self.auth.auth()
if err:
logger.error("Authentication failed: {}".format(err))
console.print("[red]Authentication failed!!!")
sys.exit(0)

# fetch all databases before starting gsql shell
with console.status(
"Preparing and personalizing GSQL for you ....", spinner="bouncingBall"
):
if self.api is None:
self.api = ApiHandler(Auth.get_creds())
result = self.api.getAllSpreadsheetInfo()
self.sqlite_manager._write_to_common(result)
self.shell_instance = GSQLShell()
self.shell_instance.cmdloop()

def error_(self):
Expand Down
Loading