1
1
from copy import copy
2
2
from functools import reduce
3
- from typing import Any , List , Optional , Sequence , Tuple as TypedTuple , Type , Union
3
+ from typing import Any , List , Optional , Sequence , Tuple as TypedTuple , Type , Union , Set , Dict
4
4
5
5
from pypika .enums import Dialects , JoinType , ReferenceOption , SetOperation
6
6
from pypika .terms import (
@@ -750,7 +750,7 @@ def __init__(
750
750
self ._select_star_tables = set ()
751
751
self ._mysql_rollup = False
752
752
self ._select_into = False
753
-
753
+ self . _using_insert_dict = False
754
754
self ._subquery_count = 0
755
755
self ._foreign_table = False
756
756
@@ -890,6 +890,9 @@ def columns(self, *terms: Any) -> "QueryBuilder":
890
890
if self ._insert_table is None :
891
891
raise AttributeError ("'Query' object has no attribute '%s'" % "insert" )
892
892
893
+ if self ._using_insert_dict :
894
+ raise QueryException ("Cannot mix use of columns() and insert_dict()" )
895
+
893
896
if terms and isinstance (terms [0 ], (list , tuple )):
894
897
terms = terms [0 ]
895
898
@@ -903,6 +906,15 @@ def insert(self, *terms: Any) -> "QueryBuilder":
903
906
self ._apply_terms (* terms )
904
907
self ._replace = False
905
908
909
+ def insert_dict (self , data : Dict [str , Any ]) -> "QueryBuilder" :
910
+ cols = data .keys ()
911
+ if self ._columns and self ._columns != cols :
912
+ raise QueryException ("Current columns differs from columns in keys" )
913
+
914
+ builder = self .columns (* cols ).insert (* data .values ())
915
+ builder ._using_insert_dict = True
916
+ return builder
917
+
906
918
@builder
907
919
def replace (self , * terms : Any ) -> "QueryBuilder" :
908
920
self ._apply_terms (* terms )
0 commit comments