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

Use SQLite for Logger #34

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open

Use SQLite for Logger #34

wants to merge 17 commits into from

Conversation

MicaelCarvalho
Copy link
Collaborator

Work in progress

@jbegaint jbegaint requested a review from Cadene May 10, 2020 23:02
@Cadene Cadene requested a review from jbegaint May 10, 2020 23:45
@@ -176,7 +177,7 @@ def print_subitem(prefix, subdictionary, stack_displacement=3):
def _execute(self, statement, parameters=None, commit=True):
parameters = parameters or ()
if not isinstance(parameters, tuple):
parameters = (parameters)
parameters = (parameters,)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch ;)

Copy link
Collaborator Author

@MicaelCarvalho MicaelCarvalho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice set of changes :)

@@ -207,39 +208,43 @@ def _list_columns(self, table_name):
table_name = self._get_internal_table_name(table_name)
query = "SELECT name FROM PRAGMA_TABLE_INFO(?)"
qry_cur = self._run_query(query, (table_name,))
columns = [res[0] for res in qry_cur]
columns = (res[0] for res in qry_cur)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason for this change?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's no need to have 2 lists, since its just an intermediate variable, a generator is just fine imo ;-)

statement = "ALTER TABLE ? ADD ? {}".format(self._get_data_type(value_sample))
return self._execute(statement, (table_name, column_name))
value_type = self._get_data_type(value_sample)
statement = f'ALTER TABLE {table_name} ADD COLUMN "{column_name}" {value_type}'
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not SQL safe; any reason for the change?

Copy link
Collaborator

@jbegaint jbegaint May 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, you can't use parameters substitution for table and columns names in sqlite, only for values

if isinstance(value, dict):
self._flatten_dict(value, flatten_dict, prefix=local_prefix)
elif not isinstance(value, (float, int)):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to be careful about double standards here: numbers.Number vs float, int

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, i'd prefer to have numbers.Number everywhere but I can change with (float, int) if you prefer ;-)

table_name = self._get_internal_table_name(table_name)
column_string = ', '.join(columns)
value_placeholder = ', '.join(['?'] * len(columns))
statement = f'INSERT INTO ?({column_string}) VALUES({value_placeholder})'
statement = f'INSERT INTO {table_name} ({column_string}) VALUES({value_placeholder})'
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also not SQL safe. Is this a requirement for table names?

Copy link
Collaborator

@jbegaint jbegaint May 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, you can't use parameters substitution for table and columns names in sqlite, only for values

for c in columns:
if c not in table_columns:
self.log_message(f'Unknown column "{c}"', log_level=self.ERROR)
column_string = ', '.join([f'"{c}"' for c in columns])
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also not SQL safe. But I guess we'll have to accept columns and table names are passive to injection...

Copy link
Collaborator

@jbegaint jbegaint May 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, you can't use parameters substitution for table and columns names in sqlite, only for values

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we could maintain a state with the current valid tables in the database...

@Cadene Cadene mentioned this pull request Jul 17, 2020
@Cadene Cadene force-pushed the MicaelCarvalho/sqlite branch from da363ec to 7782b4c Compare January 29, 2021 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants