Skip to content

Commit

Permalink
Merge pull request #433 from nextghost/tablenames
Browse files Browse the repository at this point in the history
Table name bugfixes
  • Loading branch information
gi0baro authored Jan 30, 2017
2 parents 461a854 + 0c27f3d commit b04b33e
Show file tree
Hide file tree
Showing 22 changed files with 479 additions and 209 deletions.
41 changes: 11 additions & 30 deletions pydal/adapters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,19 +346,6 @@ def drop_table(self, table, mode=''):
def rowslice(self, rows, minimum=0, maximum=None):
return rows

def alias(self, table, alias):
other = copy.copy(table)
other['_ot'] = other._ot or other.sqlsafe
other['ALL'] = SQLALL(other)
other['_tablename'] = alias
for fieldname in other.fields:
other[fieldname] = copy.copy(other[fieldname])
other[fieldname]._tablename = alias
other[fieldname].tablename = alias
other[fieldname].table = other
table._db[alias] = other
return other


class DebugHandler(ExecutionHandler):
def before_execute(self, command):
Expand Down Expand Up @@ -423,14 +410,10 @@ def execute(self, *args, **kwargs):
def _expand(self, expression, field_type=None, colnames=False,
query_env={}):
if isinstance(expression, Field):
et = expression.table
if not colnames:
table_rname = et.query_alias
rv = '%s.%s' % (table_rname, expression._rname or
(self.dialect.quote(expression.name)))
rv = expression.sqlsafe
else:
rv = '%s.%s' % (self.dialect.quote(et._tablename),
self.dialect.quote(expression.name))
rv = expression.longname
if field_type == 'string' and expression.type not in (
'string', 'text', 'json', 'password'):
rv = self.dialect.cast(rv, self.types['text'], query_env)
Expand Down Expand Up @@ -465,7 +448,7 @@ def _expand(self, expression, field_type=None, colnames=False,
def _expand_for_index(self, expression, field_type=None, colnames=False,
query_env={}):
if isinstance(expression, Field):
return expression._rname or self.dialect.quote(expression.name)
return expression._rname
return self._expand(expression, field_type, colnames, query_env)

@contextmanager
Expand All @@ -480,10 +463,10 @@ def lastrowid(self, table):
def _insert(self, table, fields):
if fields:
return self.dialect.insert(
table.sqlsafe,
','.join(el[0].sqlsafe_name for el in fields),
table._rname,
','.join(el[0]._rname for el in fields),
','.join(self.expand(v, f.type) for f, v in fields))
return self.dialect.insert_empty(table.sqlsafe)
return self.dialect.insert_empty(table._rname)

def insert(self, table, fields):
query = self._insert(table, fields)
Expand Down Expand Up @@ -511,17 +494,16 @@ def insert(self, table, fields):

def _update(self, table, query, fields):
sql_q = ''
tablename = table.sqlsafe
query_env = dict(current_scope=[table._tablename])
if query:
if use_common_filters(query):
query = self.common_filter(query, [table])
sql_q = self.expand(query, query_env=query_env)
sql_v = ','.join([
'%s=%s' % (field.sqlsafe_name,
'%s=%s' % (field._rname,
self.expand(value, field.type, query_env=query_env))
for (field, value) in fields])
return self.dialect.update(tablename, sql_v, sql_q)
return self.dialect.update(table, sql_v, sql_q)

def update(self, table, query, fields):
sql = self._update(table, query, fields)
Expand All @@ -539,13 +521,12 @@ def update(self, table, query, fields):

def _delete(self, table, query):
sql_q = ''
tablename = table.sqlsafe
query_env = dict(current_scope=[table._tablename])
if query:
if use_common_filters(query):
query = self.common_filter(query, [table])
sql_q = self.expand(query, query_env=query_env)
return self.dialect.delete(tablename, sql_q)
return self.dialect.delete(table, sql_q)

def delete(self, table, query):
sql = self._delete(table, query)
Expand Down Expand Up @@ -703,7 +684,7 @@ def _select_wcols(self, query, fields, left=False, join=False,
if (limitby and not groupby and query_tables and orderby_on_limitby and
not orderby):
sql_ord = ', '.join([
tablemap[t].sqlsafe + '.' + tablemap[t][x].sqlsafe_name
tablemap[t][x].sqlsafe
for t in query_tables if not isinstance(tablemap[t], Select)
for x in (hasattr(tablemap[t], '_primarykey') and
tablemap[t]._primarykey or ['_id'])
Expand Down Expand Up @@ -838,7 +819,7 @@ def truncate(self, table, mode=''):

def create_index(self, table, index_name, *fields, **kwargs):
expressions = [
field.sqlsafe_name if isinstance(field, Field) else field
field._rname if isinstance(field, Field) else field
for field in fields]
sql = self.dialect.create_index(
index_name, table, expressions, **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion pydal/adapters/firebird.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def lastrowid(self, table):
return long(self.cursor.fetchone()[0])

def create_sequence_and_triggers(self, query, table, **args):
tablename = table._tablename
tablename = table._rname
sequence_name = table._sequence_name
trigger_name = table._trigger_name
self.execute(query)
Expand Down
2 changes: 1 addition & 1 deletion pydal/adapters/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def select(self, query, fields, attributes):
(t.name == 'nativeRef' and item) or getattr(item, t.name)
for t in fields
] for item in items]
colnames = ['%s.%s' % (table._tablename, t.name) for t in fields]
colnames = [t.longname for t in fields]
processor = attributes.get('processor', self.parse)
return processor(rows, fields, colnames, False)

Expand Down
6 changes: 3 additions & 3 deletions pydal/adapters/ingres.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ def create_sequence_and_triggers(self, query, table, **args):
# Older Ingres releases could use rule/trigger like Oracle above.
if hasattr(table, '_primarykey'):
modify_tbl_sql = 'modify %s to btree unique on %s' % \
(table._tablename,
(table._rname,
', '.join(["'%s'" % x for x in table.primarykey]))
self.execute(modify_tbl_sql)
else:
tmp_seqname = '%s_iisq' % table._tablename
tmp_seqname = '%s_iisq' % table._raw_rname
query = query.replace(self.dialect.INGRES_SEQNAME, tmp_seqname)
self.execute('create sequence %s' % tmp_seqname)
self.execute(query)
self.execute(
'modify %s to btree unique on %s' % (table._tablename, 'id'))
'modify %s to btree unique on %s' % (table._rname, 'id'))


@adapters.register_for('ingresu')
Expand Down
2 changes: 1 addition & 1 deletion pydal/adapters/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ def __select(self, query, fields, left=False, join=False, distinct=False,
# Mongodb reserved uuid key
colname = (tablename + '.' + 'id', '_id')
else:
colname = (tablename + '.' + field.name, field.name)
colname = (field.longname, field.name)
elif not isinstance(query, Expression):
colname = (field.name, field.name)
colnames.append(colname[1])
Expand Down
14 changes: 7 additions & 7 deletions pydal/adapters/oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def lastrowid(self, table):
return long(self.cursor.fetchone()[0])

def create_sequence_and_triggers(self, query, table, **args):
tablename = table._rname or table._tablename
id_name = table._id.name
tablename = table._rname
id_name = table._id._rname
sequence_name = table._sequence_name
trigger_name = table._trigger_name
self.execute(query)
Expand Down Expand Up @@ -94,21 +94,21 @@ def sqlsafe_table(self, tablename, original_tablename=None):

def _build_value_for_insert(self, field, value, r_values):
if field.type is 'text':
r_values[':' + field.sqlsafe_name] = self.expand(value, field.type)
return ':' + field.sqlsafe_name
r_values[':' + field._rname] = self.expand(value, field.type)
return ':' + field._rname
return self.expand(value, field.type)

def _insert(self, table, fields):
if fields:
r_values = {}
return self.dialect.insert(
table.sqlsafe,
','.join(el[0].sqlsafe_name for el in fields),
table._rname,
','.join(el[0]._rname for el in fields),
','.join(
self._build_value_for_insert(f, v, r_values)
for f, v in fields)
), r_values
return self.dialect.insert_empty(table.sqlsafe), None
return self.dialect.insert_empty(table._rname), None

def insert(self, table, fields):
query, values = self._insert(table, fields)
Expand Down
8 changes: 4 additions & 4 deletions pydal/adapters/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ def _insert(self, table, fields):
retval = None
if hasattr(table, '_id'):
self._last_insert = (table._id, 1)
retval = table._id.name
retval = table._id._rname
return self.dialect.insert(
table.sqlsafe,
','.join(el[0].sqlsafe_name for el in fields),
table._rname,
','.join(el[0]._rname for el in fields),
','.join(self.expand(v, f.type) for f, v in fields),
retval)
return self.dialect.insert_empty(table.sqlsafe)
return self.dialect.insert_empty(table._rname)

@with_connection
def prepare(self, key):
Expand Down
2 changes: 1 addition & 1 deletion pydal/adapters/sap.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ def create_sequence_and_triggers(self, query, table, **args):
self.execute('CREATE SEQUENCE %s;' % table._sequence_name)
self.execute(
"ALTER TABLE %s ALTER COLUMN %s SET DEFAULT NEXTVAL('%s');" %
(table._tablename, table._id.name, table._sequence_name))
(table._rname, table._id._rname, table._sequence_name))
self.execute(query)
2 changes: 1 addition & 1 deletion pydal/adapters/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def delete(self, table, query):
counter = super(SQLite, self).delete(table, query)
if counter:
for field in table._referenced_by:
if field.type == 'reference ' + table._tablename \
if field.type == 'reference ' + table._dalname \
and field.ondelete == 'CASCADE':
db(field.belongs(deleted)).delete()
return counter
Expand Down
10 changes: 6 additions & 4 deletions pydal/contrib/imap_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ def select(self, query, fields, attributes):
fetch_results = list()

if isinstance(query, Query):
tablename = self.get_table(query)
tablename = self.get_table(query)._dalname
mailbox = self.connection.mailbox_names.get(tablename, None)
if mailbox is None:
raise ValueError("Mailbox name not found: %s" % mailbox)
Expand Down Expand Up @@ -601,7 +601,7 @@ def select(self, query, fields, attributes):
if allfields:
colnames = ["%s.%s" % (tablename, field) for field in self.search_fields.keys()]
else:
colnames = ["%s.%s" % (tablename, field.name) for field in fields]
colnames = [field.longname for field in fields]

for k in colnames:
imapfields_dict[k] = k
Expand Down Expand Up @@ -800,10 +800,11 @@ def add_payload(message, obj):
else:
raise NotImplementedError("IMAP empty insert is not implemented")

def update(self, tablename, query, fields):
def update(self, table, query, fields):
# TODO: the adapter should implement an .expand method
commands = list()
rowcount = 0
tablename = table._dalname
if use_common_filters(query):
query = self.common_filter(query, [tablename,])
mark = []
Expand Down Expand Up @@ -855,8 +856,9 @@ def count(self,query,distinct=None):
counter = len(store_list)
return counter

def delete(self, tablename, query):
def delete(self, table, query):
counter = 0
tablename = table._dalname
if query:
if use_common_filters(query):
query = self.common_filter(query, [tablename,])
Expand Down
15 changes: 10 additions & 5 deletions pydal/dialects/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,15 @@ def insert_empty(self, table):
def where(self, query):
return 'WHERE %s' % query

def update(self, tablename, values, where=None):
def update(self, table, values, where=None):
tablename = self.writing_alias(table)
whr = ''
if where:
whr = ' %s' % self.where(where)
return 'UPDATE %s SET %s%s;' % (tablename, values, whr)

def delete(self, tablename, where=None):
def delete(self, table, where=None):
tablename = self.writing_alias(table)
whr = ''
if where:
whr = ' %s' % self.where(where)
Expand Down Expand Up @@ -470,18 +472,18 @@ def primary_key(self, key):
return 'PRIMARY KEY(%s)' % key

def drop_table(self, table, mode):
return ['DROP TABLE %s;' % table.sqlsafe]
return ['DROP TABLE %s;' % table._rname]

def truncate(self, table, mode=''):
if mode:
mode = " %s" % mode
return ['TRUNCATE TABLE %s%s;' % (table.sqlsafe, mode)]
return ['TRUNCATE TABLE %s%s;' % (table._rname, mode)]

def create_index(self, name, table, expressions, unique=False):
uniq = ' UNIQUE' if unique else ''
with self.adapter.index_expander():
rv = 'CREATE%s INDEX %s ON %s (%s);' % (
uniq, self.quote(name), table.sqlsafe, ','.join(
uniq, self.quote(name), table._rname, ','.join(
self.expand(field) for field in expressions))
return rv

Expand All @@ -494,6 +496,9 @@ def constraint_name(self, table, fieldname):
def concat_add(self, tablename):
return ', ADD '

def writing_alias(self, table):
return table.sql_fullref


class NoSQLDialect(CommonDialect):
@sqltype_for('string')
Expand Down
4 changes: 2 additions & 2 deletions pydal/dialects/firebird.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ def select(self, fields, tables, where=None, groupby=None, having=None,
def drop_table(self, table, mode):
sequence_name = table._sequence_name
return [
'DROP TABLE %s %s;' % (table.sqlsafe, mode),
'DROP TABLE %s %s;' % (table._rname, mode),
'DROP GENERATOR %s;' % sequence_name]

def truncate(self, table, mode=''):
return [
'DELETE FROM %s;' % table._tablename,
'DELETE FROM %s;' % table._rname,
'SET GENERATOR %s TO 0;' % table._sequence_name]
23 changes: 19 additions & 4 deletions pydal/dialects/mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ def type_reference_fk(self):

@sqltype_for('reference TFK')
def type_reference_tfk(self):
return ' CONSTRAINT FK_%(foreign_table)s_PK FOREIGN KEY ' + \
return ' CONSTRAINT FK_%(constraint_name)s_PK FOREIGN KEY ' + \
'(%(field_name)s) REFERENCES %(foreign_table)s ' + \
'(%(foreign_key)s) ON DELETE %(on_delete_action)s',
'(%(foreign_key)s) ON DELETE %(on_delete_action)s'

@sqltype_for('geometry')
def type_geometry(self):
Expand All @@ -90,6 +90,21 @@ def type_geography(self):
def varquote(self, val):
return varquote_aux(val, '[%s]')

def update(self, table, values, where=None):
tablename = self.writing_alias(table)
whr = ''
if where:
whr = ' %s' % self.where(where)
return 'UPDATE %s SET %s FROM %s%s;' % (
table.sql_shortref, values, tablename, whr)

def delete(self, table, where=None):
tablename = self.writing_alias(table)
whr = ''
if where:
whr = ' %s' % self.where(where)
return 'DELETE %s FROM %s%s;' % (table.sql_shortref, tablename, whr)

def select(self, fields, tables, where=None, groupby=None, having=None,
orderby=None, limitby=None, distinct=False, for_update=False):
dst, whr, grp, order, limit, offset, upd = '', '', '', '', '', '', ''
Expand Down Expand Up @@ -180,7 +195,7 @@ def concat_add(self, tablename):
return '; ALTER TABLE %s ADD ' % tablename

def drop_index(self, name, table):
return 'DROP INDEX %s ON %s;' % (self.quote(name), table.sqlsafe)
return 'DROP INDEX %s ON %s;' % (self.quote(name), table._rname)

def st_astext(self, first, query_env={}):
return '%s.STAsText()' % self.expand(first, query_env=query_env)
Expand Down Expand Up @@ -393,7 +408,7 @@ def extract(self, first, what, query_env={}):
def truncate(self, table, mode=''):
if mode:
mode = " %s" % mode
return ['TRUNCATE %s%s;' % (table.sqlsafe, mode)]
return ['TRUNCATE %s%s;' % (table._rname, mode)]

def select(self, *args, **kwargs):
return SQLDialect.select(self, *args, **kwargs)
Expand Down
Loading

0 comments on commit b04b33e

Please sign in to comment.