Beispiel #1
0
 def delete_all(self):
     """
 Delete all rows of objects stored in this query. Only if the query are made
 in T_CLASS format, otherwise will rise an AttributeError exception.
 :return:
     """
     count = 0
     connection = self._obj.create_connection()
     for obj in self._resultset:
         count_attr = 0
         data = normalize_db_values(obj._data_dict(), self._obj)
         cmd = "DELETE FROM {} WHERE".format(self._obj.table_name)
         for key in data:
             if data[key] is None:
                 cmd += ' {} is %({})s AND'.format(
                     obj.normalize_column(key), key)
             else:
                 cmd += ' {}=%({})s AND'.format(obj.normalize_column(key),
                                                key)
             count_attr += 1
         if cmd.endswith(' AND'):
             cmd = cmd[0:len(cmd) - 4]
         connection.command(cmd, data)
         count += 1
     connection.close()
     self._resultset = ()
     return count
Beispiel #2
0
 def get(self, result_type=T_CLASS, **where_clauses):
     """
 Get objects from specifieds clauses.
 :param result_type: type to store in resultset:
                     'class' for a model.
                     'dict' for a dictionary
                     'list' for a list.
 :param where_clauses: clauses according with model fields.
 :return:
     """
     connection = self._obj.create_connection()
     query = replace_when_none(nwe(self._custom_qry_init_part), self._qry_init_part)
     where_clauses = normalize_db_values(where_clauses, self._obj)
     self._resultset = ()
     result_type = T_DICT if self._custom_qry_init_part else result_type
     if len(where_clauses) == 0:
         query += ' 1=1'
     else:
         for key in where_clauses.keys():
             sep = '__'
             if sep in key:
                 opt = key.split(sep)[-1]
                 if opt in self._where_opt:
                     if where_clauses[key] is None and opt == 'not':
                         query += ' {} is not %({})s ' \
                                  'AND'.format(self._obj.normalize_column(
                             key.split(sep + opt)[0]), key)
                     else:
                         query += ' {} {} %({})s AND'.format(
                             self._obj.normalize_column(
                                 key.split(sep + opt)[0]),
                             self._where_opt[opt], key)
                     continue
             if where_clauses[key] is None:
                 query += ' {} is %({})s AND'.format(
                     self._obj.normalize_column(key), key)
             else:
                 query += ' {} = %({})s AND'.format(
                     self._obj.normalize_column(key), key)
     query = query.strip('AND') + self._order_by
     if result_type == T_CLASS:
         self._populate_dict(connection.query_with_columns(query,
                                                           where_clauses))
     elif result_type == T_DICT:
         self._populate_dict(
             connection.query_with_columns(query, where_clauses),
             result_type)
     else:
         self._populate(connection.query(query, where_clauses))
     connection.close()
     self._order_by = ''
     self._custom_qry_init_part = ''
     return self