Beispiel #1
0
 def _check_limit(self, limit):
     error = 'Wrong limit "%s". Limit has to be a positive integer or convertable to integer.' \
             % limit
     if not limit:
         raise PyPgException(error)
     try:
         return int(limit)
     except ValueError:
         raise PyPgException(error)
Beispiel #2
0
 def get_foreign_key_for_table(table, foreign_table):
     try:
         return Structure.get_foreign_keys_for_table(table,
                                                     foreign_table)[0]
     except IndexError:
         raise PyPgException('Table %s has no foreign key for table %s' %
                             (table, foreign_table))
Beispiel #3
0
 def _get_pk(self):
     pk = Structure.get_primary_key(self._table_name)
     if self.data.get(pk) is not None:
         return pk
     else:
         raise PyPgException(
             'Incorectly formated naming for primary key. Please provide manager.Naming instance or set strict mode on.'
         )
Beispiel #4
0
 def _validate_name(self, name):
     cursor = self.conn.cursor()
     cursor.execute(
         "select (1) from information_schema.tables where table_name=%s",
         [name])
     exists = cursor.fetchall()
     cursor.close()
     self.conn.commit()
     if len(exists):
         raise PyPgException("Table with that name already exists")
Beispiel #5
0
 def tables_related(table, reltable):
     if settings.STRICT:
         if reltable in Manager.get_scheme(
         )[table]['fks'] or table in Manager.get_scheme()[reltable]['fks']:
             return True
         else:
             raise PyPgException('Tables %s and %s are not related.' %
                                 (table, reltable))
     else:
         return True
Beispiel #6
0
 def set_naming(naming):
     from structure import Naming
     if naming:
         if not isinstance(naming, Naming):
             raise PyPgException(
                 'Naming must be instance of class utils.Naming')
         else:
             Manager._NAMING = naming
     else:
         Manager._NAMING = Naming()
Beispiel #7
0
 def get_fk_referenced_table(table, foreign_key):
     if settings.STRICT:
         for table, fks in Manager.get_scheme()[table]['fks'].items():
             if foreign_key in fks['relcolumns']:
                 return table
         else:
             raise PyPgException('Table %s has no foreign key %s.' %
                                 (table, foreign_key))
     else:
         return Manager.get_naming().get_fk_column(table, foreign_key)
Beispiel #8
0
 def update(self, **kwargs):
     self._check_deleted()
     if self._changed or kwargs:
         map(self._check_column_in_table, kwargs.keys())
         self._sql.add_update_kwargs(kwargs)
         Query().execute(**self._sql.build_update())
         self._changed = False
         self._set_sql_builder()
         return self
     else:
         raise PyPgException('No data to update for this row.')
Beispiel #9
0
 def table_exists(table):
     if settings.STRICT:
         if table in Manager.get_scheme():
             return True
         else:
             if settings.DEBUG:
                 tables = ' Choices are: %s' % ', '.join(
                     Structure.get_all_tables())
             else:
                 tables = ''
             raise PyPgException('No table "%s" in database.%s' %
                                 (table, tables))
     else:
         return True
Beispiel #10
0
 def table_has_column(table, column):
     if settings.STRICT:
         if column in Manager.get_scheme()[table]['columns']:
             return True
         else:
             if settings.DEBUG:
                 columns = ' Choices are: %s' % ', '.join(
                     Structure.get_all_columns(table))
             else:
                 columns = ''
             error = 'Column "%s" is not a valid column in table "%s".%s' \
                     % (column, table, columns)
             raise PyPgException(error)
     else:
         return True
Beispiel #11
0
 def get_connection():
     if Manager._CONNECTION:
         return Manager._CONNECTION
     else:
         raise PyPgException('No connection specified.')
Beispiel #12
0
 def _check_deleted(self):
     if self._deleted:
         raise PyPgException('This row was deleted.')
Beispiel #13
0
 def _check_is_instance(self, instance, class_):
     if not instance.__class__.__name__ == class_:
         raise PyPgException('Argument "%s" is not "%s" instance.' %
                             (instance, class_))
     return True
Beispiel #14
0
 def _validate_on(self, table, on):
     if not isinstance(on._value, on._column.__class__):
         raise PyPgException("Wrong join condition.")
Beispiel #15
0
 def _validate_args_kwargs(self, args, kwargs, method, class_):
     if not args and not kwargs:
         raise PyPgException(self._NO_ARGUMENTS_ERROR % method)
     map(lambda arg: self._check_is_instance(arg, class_), args)
     map(self._check_column_in_table, kwargs.keys())
Beispiel #16
0
 def _validate_kwargs(self, kwargs, method):
     if not kwargs:
         raise PyPgException(self._NO_ARGUMENTS_ERROR % method)
     map(self._check_column_in_table, kwargs.keys())
Beispiel #17
0
 def get_primary_key(table):
     try:
         return Structure.get_primary_keys(table)[0]
     except IndexError:
         raise PyPgException('Table %s has no primary key.' % table)
Beispiel #18
0
 def get_scheme():
     if not Manager._CACHE_POPULATED:
         raise PyPgException(
             'Cache is old or has never been popuated. Please renew cache.')
     else:
         return Manager._INTROSPECTION_CACHE