Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed review issues noted by claudep. Refs #17788.

The issues fixed were some comment issues, use of '//' operator instead
of '/' operator. In addition, added some defensive programming
enchantments for division by zero and zero sized batches.
  • Loading branch information...
commit 332fb9308c39249cdd38a034a3565778636c373b 1 parent d4eacac
Anssi Kääriäinen authored June 04, 2012
7  django/db/backends/__init__.py
@@ -478,13 +478,10 @@ def autoinc_sql(self, table, column):
478 478
     def bulk_batch_size(self, fields, objs):
479 479
         """
480 480
         Returns the maximum allowed batch size for the backend. The fields
481  
-        are the fields we will be inserting in the batch, the objs contains
  481
+        are the fields going to be inserted in the batch, the objs contains
482 482
         all the objects to be inserted.
483  
-
484  
-        The default return value of None means unlimited. This method must
485  
-        always return a valuer greater than 0.
486 483
         """
487  
-        return None
  484
+        return len(objs)
488 485
 
489 486
     def date_extract_sql(self, lookup_type, field_name):
490 487
         """
5  django/db/backends/sqlite3/base.py
@@ -106,9 +106,10 @@ def _supports_stddev(self):
106 106
 class DatabaseOperations(BaseDatabaseOperations):
107 107
     def bulk_batch_size(self, fields, objs):
108 108
         """
109  
-        SQLite has a limit of 1000 variables per query.
  109
+        SQLite has a compile-time default (SQLITE_LIMIT_VARIABLE_NUMBER) of
  110
+        999 variables per query. 
110 111
         """
111  
-        return max(999 / len(fields), 1)
  112
+        return (999 // len(fields)) if len(fields) > 0 else len(objs)
112 113
 
113 114
     def date_extract_sql(self, lookup_type, field_name):
114 115
         # sqlite doesn't support extract, so we fake it with the user-defined
6  django/db/models/query.py
@@ -871,10 +871,8 @@ def _batched_insert(self, objs, fields, batch_size):
871 871
         """
872 872
         if not objs:
873 873
             return
874  
-        connection = connections[self.db]
875  
-        batch_size = (batch_size
876  
-                      or connection.ops.bulk_batch_size(fields, objs)
877  
-                      or len(objs))
  874
+        ops = connections[self.db].ops
  875
+        batch_size = (batch_size or max(ops.bulk_batch_size(fields, objs), 1))
878 876
         for batch in [objs[i:i+batch_size]
879 877
                       for i in range(0, len(objs), batch_size)]:
880 878
             self.model._base_manager._insert(batch, fields=fields,

0 notes on commit 332fb93

Please sign in to comment.
Something went wrong with that request. Please try again.