Skip to content
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

Insert, delete, and update in google spreadsheet: issue#5 #13

Open
wants to merge 2 commits 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: 3 additions & 1 deletion gsheetsdb/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

# Google API scopes for authentication
# https://developers.google.com/chart/interactive/docs/spreadsheets
SCOPES = ['https://spreadsheets.google.com/feeds']
# SCOPES = ['https://spreadsheets.google.com/feeds'] original

SCOPES = ['https://www.googleapis.com/auth/spreadsheets']


def get_credentials_from_auth(
Expand Down
32 changes: 26 additions & 6 deletions gsheetsdb/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from gsheetsdb.query import execute
from gsheetsdb.sqlite import execute as sqlite_execute

import sqlparse
from gsheetsdb.url import url_from_sql
from gsheetsdb.sqlite import execute_all_sql

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -130,15 +133,32 @@ def close(self):

@check_closed
def execute(self, operation, parameters=None, headers=0):

self.description = None
query = apply_parameters(operation, parameters or {})
try:
self._results, self.description = execute(
query, headers, self.credentials)
except (ProgrammingError, NotSupportedError):
logger.info('Query failed, running in SQLite')
self._results, self.description = sqlite_execute(

# Use switch cases for select, insert, update, delete (JG)
# Parse query to extract SQL statement/query type
parsed = sqlparse.parse(query)[0]
parsed_token = parsed.tokens

# Execute only for 'SELECT' query
if str(parsed_token[0]) == 'SELECT':
try:
self._results, self.description = execute(
query, headers, self.credentials)

except (ProgrammingError, NotSupportedError):
logger.info('Query failed, running in SQLite')
self._results, self.description = sqlite_execute(
query, headers, self.credentials)

# Execute when statement is other than 'SELECT'
else:
# Execute all SQL statements other than 'SELECT'
execute_all_sql(query, headers, self.credentials)
exit()

return self

@check_closed
Expand Down
Loading