@@ -2877,7 +2877,7 @@ def insert_chunk(
28772877 num_records_processed ,
28782878 replace ,
28792879 ignore ,
2880- ):
2880+ ) -> List [ Dict ] :
28812881 queries_and_params = self .build_insert_queries_and_params (
28822882 extracts ,
28832883 chunk ,
@@ -2893,68 +2893,24 @@ def insert_chunk(
28932893 ignore ,
28942894 )
28952895
2896- result = None
2896+ records = []
28972897 for query , params in queries_and_params :
2898- try :
2899- result = self .db .execute (query , tuple (params ))
2900- except OperationalError as e :
2901- if alter and (" column" in e .args [0 ]):
2902- # Attempt to add any missing columns, then try again
2903- self .add_missing_columns (chunk )
2904- result = self .db .execute (query , params )
2905- elif e .args [0 ] == "too many SQL variables" :
2906- first_half = chunk [: len (chunk ) // 2 ]
2907- second_half = chunk [len (chunk ) // 2 :]
2908-
2909- self .insert_chunk (
2910- alter ,
2911- extracts ,
2912- first_half ,
2913- all_columns ,
2914- hash_id ,
2915- hash_id_columns ,
2916- upsert ,
2917- pk ,
2918- not_null ,
2919- conversions ,
2920- num_records_processed ,
2921- replace ,
2922- ignore ,
2923- )
2924-
2925- self .insert_chunk (
2926- alter ,
2927- extracts ,
2928- second_half ,
2929- all_columns ,
2930- hash_id ,
2931- hash_id_columns ,
2932- upsert ,
2933- pk ,
2934- not_null ,
2935- conversions ,
2936- num_records_processed ,
2937- replace ,
2938- ignore ,
2939- )
2898+ cursor = self .db .execute (query , tuple (params ))
2899+ columns = [c [0 ] for c in cursor .description ]
2900+ record = dict (zip (columns , cursor .fetchone ()))
2901+ records .append (record )
29402902
2903+ # Preserve old self.last_pk functionality
2904+ if (hash_id or pk ) and not upsert :
2905+ if hash_id :
2906+ self .last_pk = row [hash_id ]
2907+ elif isinstance (pk , str ):
2908+ self .last_pk = row [pk ]
29412909 else :
2942- raise
2943- if num_records_processed == 1 :
2944- rid = self .db .get_last_rowid ()
2945- if rid is not None :
2946- self .last_pk = self .last_rowid = rid
2947- # self.last_rowid will be 0 if a "INSERT OR IGNORE" happened
2948- if (hash_id or pk ) and not upsert :
2949- row = list (self .rows_where ("rowid = ?" , [rid ]))[0 ]
2950- if hash_id :
2951- self .last_pk = row [hash_id ]
2952- elif isinstance (pk , str ):
2953- self .last_pk = row [pk ]
2954- else :
2955- self .last_pk = tuple (row [p ] for p in pk )
2956-
2957- return
2910+ self .last_pk = tuple (row [p ] for p in pk )
2911+ self .last_rowid = self .last_pk
2912+
2913+ return records
29582914
29592915 def insert (
29602916 self ,
@@ -2973,7 +2929,7 @@ def insert(
29732929 conversions : Optional [Union [Dict [str , str ], Default ]] = DEFAULT ,
29742930 columns : Optional [Union [Dict [str , Any ], Default ]] = DEFAULT ,
29752931 strict : Optional [Union [bool , Default ]] = DEFAULT ,
2976- ) -> "Table" :
2932+ ) -> Dict :
29772933 """
29782934 Insert a single record into the table. The table will be created with a schema that matches
29792935 the inserted record if it does not already exist, see :ref:`python_api_creating_tables`.
@@ -3023,7 +2979,7 @@ def insert(
30232979 conversions = conversions ,
30242980 columns = columns ,
30252981 strict = strict ,
3026- )
2982+ )[ 0 ]
30272983
30282984 def insert_all (
30292985 self ,
@@ -3046,7 +3002,7 @@ def insert_all(
30463002 upsert = False ,
30473003 analyze = False ,
30483004 strict = DEFAULT ,
3049- ) -> "Table" :
3005+ ) -> List [ Dict ] :
30503006 """
30513007 Like ``.insert()`` but takes a list of records and ensures that the table
30523008 that it creates (if table does not exist) has columns for ALL of that data.
@@ -3105,6 +3061,7 @@ def insert_all(
31053061 self .last_pk = None
31063062 if truncate and self .exists ():
31073063 self .db .execute ("DELETE FROM [{}];" .format (self .name ))
3064+ records = []
31083065 for chunk in chunks (itertools .chain ([first_record ], records ), batch_size ):
31093066 chunk = list (chunk )
31103067 num_records_processed += len (chunk )
@@ -3139,7 +3096,7 @@ def insert_all(
31393096
31403097 first = False
31413098
3142- self .insert_chunk (
3099+ records . extend ( self .insert_chunk (
31433100 alter ,
31443101 extracts ,
31453102 chunk ,
@@ -3153,12 +3110,12 @@ def insert_all(
31533110 num_records_processed ,
31543111 replace ,
31553112 ignore ,
3156- )
3113+ ))
31573114
31583115 if analyze :
31593116 self .analyze ()
31603117
3161- return self
3118+ return records
31623119
31633120 def upsert (
31643121 self ,
0 commit comments