MCPcopy Index your code
hub / github.com/piccolo-orm/piccolo / Insert

Class Insert

piccolo/query/methods/insert.py:29–149  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

27
28
29class Insert(
30 Generic[TableInstance], Query[TableInstance, list[dict[str, Any]]]
31):
32 __slots__ = ("add_delegate", "on_conflict_delegate", "returning_delegate")
33
34 def __init__(
35 self, table: type[TableInstance], *instances: TableInstance, **kwargs
36 ):
37 super().__init__(table, **kwargs)
38 self.add_delegate = AddDelegate()
39 self.returning_delegate = ReturningDelegate()
40 self.on_conflict_delegate = OnConflictDelegate()
41 self.add(*instances)
42
43 ###########################################################################
44 # Clauses
45
46 def add(self: Self, *instances: Table) -> Self:
47 self.add_delegate.add(*instances, table_class=self.table)
48 return self
49
50 def returning(self: Self, *columns: Column) -> Self:
51 self.returning_delegate.returning(columns)
52 return self
53
54 def on_conflict(
55 self: Self,
56 target: Optional[Union[str, Column, tuple[Column, ...]]] = None,
57 action: Union[
58 OnConflictAction, Literal["DO NOTHING", "DO UPDATE"]
59 ] = OnConflictAction.do_nothing,
60 values: Optional[Sequence[Union[Column, tuple[Column, Any]]]] = None,
61 where: Optional[Combinable] = None,
62 ) -> Self:
63 if (
64 self.engine_type == "sqlite"
65 and self.table._meta.db.get_version_sync() < 3.24
66 ):
67 raise NotImplementedError(
68 "SQLite versions lower than 3.24 don't support ON CONFLICT"
69 )
70
71 if (
72 self.engine_type in ("postgres", "cockroach")
73 and len(self.on_conflict_delegate._on_conflict.on_conflict_items)
74 == 1
75 ):
76 raise NotImplementedError(
77 "Postgres and Cockroach only support a single ON CONFLICT "
78 "clause."
79 )
80
81 self.on_conflict_delegate.on_conflict(
82 target=target,
83 action=action,
84 values=values,
85 where=where,
86 )

Callers 1

insertMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected