@@ -67,6 +67,12 @@ class TemporaryTable(object):
6767 __slots__ = ("connection" , "name" , "fields" , "backend" , "_meta" )
6868
6969 def __init__ (self , connection , name , ** fields ):
70+ """
71+ :param connection: A database connection object
72+ :param name: A str name for the table in the database
73+ :param fields: Keyword arguments are assumed to be fields for defining the schema of the
74+ temporary table
75+ """
7076 self .connection = connection
7177 self .name = name
7278 self .fields = []
@@ -86,13 +92,27 @@ def __exit__(self, exc_type, exc_val, exc_tb):
8692
8793 @property
8894 def sql_name (self ):
95+ """
96+ :return: The name actual name of the table used in the DB, prefixed to avoid collisions
97+ """
8998 return self .connection .ops .quote_name ("t_{}" .format (self .name ))
9099
100+ def get_field (self , name ):
101+ """
102+ :param name: A str of the name of which field to find
103+ :return: The field object
104+ """
105+ return next (f for f in self .fields if f .name == name )
106+
91107 def create (self ):
108+ """
109+ Creates the temporary table within the database
110+ """
92111 fields = []
93112 params = []
94113 with self .connection .schema_editor () as schema_editor :
95114 for field in self .fields :
115+ # generates the SQL expression for the table column
96116 field_sql , field_params = schema_editor .column_sql (
97117 self , field , include_default = True
98118 )
@@ -103,16 +123,24 @@ def create(self):
103123 self .backend ._create_temporary_table (c , self .sql_name , fields , params )
104124
105125 def drop (self ):
126+ """
127+ Drops the temporary table within the database
128+ """
106129 with self .connection .cursor () as c :
107130 c .execute ("DROP TABLE IF EXISTS {name}" .format (name = self .sql_name ))
108131
109132 def bulk_insert (self , values ):
133+ """
134+ Bulk inserts a list of records into the temporary table
135+
136+ :param values: A list of dictionaries containing data to insert, keyed by field name
137+ """
110138 params = []
111139 for value_dict in values :
112140 for field in self .fields :
113141 params .append (value_dict .get (field .attname ))
114142 with self .connection .cursor () as c :
115- self .backend ._bulk_full_record_upsert (c , self .sql_name , self .fields , params )
143+ self .backend ._bulk_insert (c , self .sql_name , self .fields , params )
116144
117145 class Meta :
118146 """
0 commit comments