CRUD Classes
Sync CRUD
Bases: BaseCrud
, Generic[M]
Abstracts CRUD actions for model associated tables
Source code in ardilla/crud.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
|
count(column='*', /, **kws)
Returns an integer of the number of non null values in a column Or the total number of rows if '*' is passed
Parameters:
Name | Type | Description | Default |
---|---|---|---|
column |
str
|
The column name to count rows on. Defaults to '*' which counts all the rows in the table |
'*'
|
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
the number of rows with non null values in a column or the number of rows in a table |
Source code in ardilla/crud.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
|
delete_many(*objs)
Deletes all the objects passed
Parameters:
Name | Type | Description | Default |
---|---|---|---|
objs |
M
|
the object to delete |
()
|
Returns:
Type | Description |
---|---|
Literal[True]
|
The literal |
Source code in ardilla/crud.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
|
delete_one(obj)
Deletes the object from the database (won't delete the actual object) If the object has a PK field or the rowid setup, those will be used to locate the obj and delete it. If not, this function will delete any row that meets the values of the object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
M
|
the object to delete |
required |
Returns:
Type | Description |
---|---|
Literal[True]
|
The literal |
Source code in ardilla/crud.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
|
get_all()
Gets all objects from the database
Returns:
Type | Description |
---|---|
list[M]
|
A list with all the rows in table as instances of the model |
Source code in ardilla/crud.py
156 157 158 159 160 161 |
|
get_many(order_by=None, limit=None, **kws)
Queries the database and returns objects that meet the criteris
Parameters:
Name | Type | Description | Default |
---|---|---|---|
order_by |
Optional[dict[str, str]]
|
An ordering dict. Defaults to None.
The ordering should have the structure: |
None
|
limit |
Optional[int]
|
The number of items to return. Defaults to None. |
None
|
kws |
SQLFieldType
|
The column names and values for the select query |
{}
|
Returns:
Type | Description |
---|---|
list[M]
|
a list of rows matching the criteria as intences of the model |
Source code in ardilla/crud.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
|
get_or_create(**kws)
Returns an object from the database with the spefied matching data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kws |
SQLFieldType
|
the key value pairs will be used to query for an existing row if no record is found then a new row will be inserted |
{}
|
Returns:
Type | Description |
---|---|
tuple[M, bool]
|
A tuple with two values, the object and a boolean indicating if the object was newly created or not |
Source code in ardilla/crud.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
|
get_or_none(**kws)
Returns a row as an instance of the model if one is found or none
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kws |
SQLFieldType
|
The keyword arguments are passed as column names and values to a select query |
{}
|
Example
crud.get_or_none(id=42)
# returns an object with id of 42 or None if there isn't one in the database
Returns:
Type | Description |
---|---|
Optional[M]
|
The object found with the criteria if any |
Source code in ardilla/crud.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
|
insert(**kws)
Inserts a record into the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kws |
SQLFieldType
|
The keyword arguments are passed as the column names and values to the insert query |
{}
|
Returns:
Type | Description |
---|---|
M
|
Creates a new entry in the database and returns the object |
Rises
ardilla.error.QueryExecutionError
: if there's a conflict when inserting the record
Source code in ardilla/crud.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
|
insert_or_ignore(**kws)
Inserts a record to the database with the keywords passed. It ignores conflicts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kws |
SQLFieldType
|
The keyword arguments are passed as the column names and values to the insert query |
{}
|
Returns:
Type | Description |
---|---|
Optional[M]
|
The newly created row as an instance of the model if there was no conflicts |
Source code in ardilla/crud.py
100 101 102 103 104 105 106 107 108 109 110 111 |
|
save_many(*objs)
Saves all the passed objects to the database
Parameters:
Name | Type | Description | Default |
---|---|---|---|
objs |
M
|
the objects to persist |
()
|
Returns:
Type | Description |
---|---|
Literal[True]
|
The literal |
Source code in ardilla/crud.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
|
save_one(obj)
Saves one object to the database
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
M
|
the object to persist |
required |
Returns:
Type | Description |
---|---|
Literal[True]
|
The literal |
Source code in ardilla/crud.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
|
Async CRUD
Bases: BaseCrud
, Generic[M]
Abstracts CRUD actions for model associated tables
Source code in ardilla/asyncio/crud.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
|
count(column='*', /, **kws)
async
Returns an integer of the number of non null values in a column Or the total number of rows if '*' is passed
Parameters:
Name | Type | Description | Default |
---|---|---|---|
column |
str
|
The column name to count rows on. Defaults to '*' which counts all the rows in the table |
'*'
|
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
the number of rows with non null values in a column or the number of rows in a table |
Source code in ardilla/asyncio/crud.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
|
delete_many(*objs)
async
Deletes all the objects passed
Parameters:
Name | Type | Description | Default |
---|---|---|---|
objs |
M
|
the object to delete |
()
|
Returns:
Type | Description |
---|---|
Literal[True]
|
The literal |
Source code in ardilla/asyncio/crud.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
|
delete_one(obj)
async
Deletes the object from the database (won't delete the actual object) If the object has a PK field or the rowid setup, those will be used to locate the obj and delete it. If not, this function will delete any row that meets the values of the object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
M
|
the object to delete |
required |
Returns:
Type | Description |
---|---|
Literal[True]
|
The literal |
Source code in ardilla/asyncio/crud.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
|
get_all()
async
Gets all objects from the database
Returns:
Type | Description |
---|---|
list[M]
|
A list with all the rows in table as instances of the model |
Source code in ardilla/asyncio/crud.py
149 150 151 152 153 154 155 156 157 158 |
|
get_many(order_by=None, limit=None, **kws)
async
Queries the database and returns objects that meet the criteris
Parameters:
Name | Type | Description | Default |
---|---|---|---|
order_by |
Optional[dict[str, str]]
|
An ordering dict. Defaults to None.
The ordering should have the structure: |
None
|
kws |
SQLFieldType
|
the column names and values for the select query |
{}
|
limit |
Optional[int]
|
The number of items to return. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
list[M]
|
a list of rows matching the criteria as intences of the model |
Source code in ardilla/asyncio/crud.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
|
get_or_create(**kws)
async
Returns an object from the database with the spefied matching data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kws |
SQLFieldType
|
the key value pairs will be used to query for an existing row if no record is found then a new row will be inserted |
{}
|
Returns:
Type | Description |
---|---|
tuple[M, bool]
|
A tuple with two values, the object and a boolean indicating if the object was newly created or not |
Source code in ardilla/asyncio/crud.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
|
get_or_none(**kws)
async
Returns a row as an instance of the model if one is found or none
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kws |
SQLFieldType
|
The keyword arguments are passed as column names and values to a select query |
{}
|
Example
await crud.get_or_none(id=42)
# returns an object with id of 42 or None if there isn't one in the database
Returns:
Type | Description |
---|---|
Optional[M]
|
The object found with the criteria if any |
Source code in ardilla/asyncio/crud.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
insert(**kws)
async
Inserts a record into the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kws |
SQLFieldType
|
the column names and values for the insertion query |
{}
|
Returns:
Type | Description |
---|---|
M
|
Returns the inserted row as an instance of the model |
Rises
ardilla.error.QueryExecutionError: if there's a conflict when inserting the record
Source code in ardilla/asyncio/crud.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
|
insert_or_ignore(**kws)
async
Inserts a record to the database with the keywords passed. It ignores conflicts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kws |
SQLFieldType
|
The keyword arguments are passed as the column names and values to the insert query |
{}
|
Returns:
Type | Description |
---|---|
Optional[M]
|
The newly created row as an instance of the model if there was no conflicts |
Source code in ardilla/asyncio/crud.py
120 121 122 123 124 125 126 127 128 129 130 131 |
|
save_many(*objs)
async
Saves all the passed objects to the database
Parameters:
Name | Type | Description | Default |
---|---|---|---|
objs |
M
|
the objects to persist |
()
|
Returns:
Type | Description |
---|---|
Literal[True]
|
The literal |
Source code in ardilla/asyncio/crud.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
|
save_one(obj)
async
Saves one object to the database
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
M
|
the object to persist |
required |
Returns:
Type | Description |
---|---|
Literal[True]
|
The literal |
Source code in ardilla/asyncio/crud.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
|