| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent de0016b commit 51cee69
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -84,31 +84,32 @@ The two main ways of retrieving data through the ORM are ```where()``` and | |||
| 84 | 84 | ```find()```/```find_multi()```: | |
| 85 | 85 | ||
| 86 | 86 | ``` python | |
| 87 | - # where() is invokes on a model class to retrieve models of that tyep. it takes a | ||
| 88 | - # transaction and then a sequence of conditions. | ||
| 89 | - # Most conditions that specify a Field, Index, Relationship, or Model can take | ||
| 90 | - # either the name of the object or the object itself | ||
| 91 | - test_objects = TestModel.where(None, spanner_orm.greater_than('value', '50')) | ||
| 87 | + # where() is invokes on a model class to retrieve models of that type. it takes | ||
| 88 | + # a sequence of conditions. Most conditions that specify a Field, Index, | ||
| 89 | + # Relationship, or Model can take either the name of the object or the object | ||
| 90 | + # itself | ||
| 91 | + test_objects = TestModel.where(spanner_orm.greater_than('value', '50')) | ||
| 92 | 92 | ||
| 93 | 93 | # To also retrieve related objects, the includes() condition should be used: | |
| 94 | - test_and_other_objects = TestModel.where(None, | ||
| 95 | - spanner_orm.greater_than(TestModel.value, '50'), | ||
| 96 | - spanner_orm.includes(TestModel.fake_relationship)) | ||
| 94 | + test_and_other_objects = TestModel.where( | ||
| 95 | + spanner_orm.greater_than(TestModel.value, '50'), | ||
| 96 | + spanner_orm.includes(TestModel.fake_relationship), | ||
| 97 | + ) | ||
| 97 | 98 | ||
| 98 | 99 | # To create a transaction, run_read_only() or run_write() are used with the | |
| 99 | 100 | # method to be run inside the transaction and any arguments to passs to the method. | |
| 100 | 101 | # The method is invoked with the transaction as the first argument and then the | |
| 101 | 102 | # rest of the provided arguments: | |
| 102 | 103 | def callback_1(transaction, argument): | |
| 103 | - return TestModel.find(transaction, id=argument) | ||
| 104 | + return TestModel.find(id=argument, transaction=transaction) | ||
| 104 | 105 | ||
| 105 | 106 | specific_object = spanner_orm.spanner_api().run_read_only(callback, 1) | |
| 106 | 107 | ||
| 107 | 108 | # Alternatively, the transactional_read decorator can be used to clean up the | |
| 108 | 109 | # call a bit: | |
| 109 | 110 | @transactional_read | |
| 110 | 111 | def finder(argument, transaction=None): | |
| 111 | - return TestModel.find(transaction, id=argument) | ||
| 112 | + return TestModel.find(id=argument, transaction=transaction) | ||
| 112 | 113 | specific_object = finder(1) | |
| 113 | 114 | ``` | |
| 114 | 115 | ||
@@ -131,7 +132,7 @@ models = [] | |||
| 131 | 132 | for i in range(10): | |
| 132 | 133 | key = 'test_{}'.format(i) | |
| 133 | 134 | models.append(TestModel({'key': key, 'value': value})) | |
| 134 | - TestModel.save_batch(None, models) | ||
| 135 | + TestModel.save_batch(models) | ||
| 135 | 136 | ``` | |
| 136 | 137 | ||
| 137 | 138 | ```spanner_orm.spanner_api().run_write()``` can be used for executing read-write | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -71,9 +71,10 @@ def model(cls, table_name) -> Optional[Type[model.Model]]: | |||
| 71 | 71 | def tables(cls) -> Dict[str, Dict[str, Any]]: | |
| 72 | 72 | """Compiles table information from column schema.""" | |
| 73 | 73 | column_data = collections.defaultdict(dict) | |
| 74 | - columns = column.ColumnSchema.where(None, | ||
| 75 | - condition.equal_to('table_catalog', ''), | ||
| 76 | - condition.equal_to('table_schema', '')) | ||
| 74 | + columns = column.ColumnSchema.where( | ||
| 75 | + condition.equal_to('table_catalog', ''), | ||
| 76 | + condition.equal_to('table_schema', ''), | ||
| 77 | + ) | ||
| 77 | 78 | for column_row in columns: | |
| 78 | 79 | new_field = field.Field( | |
| 79 | 80 | column_row.field_type(), nullable=column_row.nullable()) | |
@@ -82,9 +83,10 @@ def tables(cls) -> Dict[str, Dict[str, Any]]: | |||
| 82 | 83 | column_data[column_row.table_name][column_row.column_name] = new_field | |
| 83 | 84 | ||
| 84 | 85 | table_data = collections.defaultdict(dict) | |
| 85 | - tables = table.TableSchema.where(None, | ||
| 86 | - condition.equal_to('table_catalog', ''), | ||
| 87 | - condition.equal_to('table_schema', '')) | ||
| 86 | + tables = table.TableSchema.where( | ||
| 87 | + condition.equal_to('table_catalog', ''), | ||
| 88 | + condition.equal_to('table_schema', ''), | ||
| 89 | + ) | ||
| 88 | 90 | for table_row in tables: | |
| 89 | 91 | name = table_row.table_name | |
| 90 | 92 | table_data[name]['parent_table'] = table_row.parent_table_name | |
@@ -98,9 +100,10 @@ def indexes(cls) -> Dict[str, Dict[str, Any]]: | |||
| 98 | 100 | # Results are ordered by that so the index columns are added in the | |
| 99 | 101 | # correct order. | |
| 100 | 102 | index_column_schemas = index_column.IndexColumnSchema.where( | |
| 101 | - None, condition.equal_to('table_catalog', ''), | ||
| 103 | + condition.equal_to('table_catalog', ''), | ||
| 102 | 104 | condition.equal_to('table_schema', ''), | |
| 103 | - condition.order_by(('ordinal_position', condition.OrderType.ASC))) | ||
| 105 | + condition.order_by(('ordinal_position', condition.OrderType.ASC)), | ||
| 106 | + ) | ||
| 104 | 107 | ||
| 105 | 108 | index_columns = collections.defaultdict(list) | |
| 106 | 109 | storing_columns = collections.defaultdict(list) | |
@@ -112,8 +115,9 @@ def indexes(cls) -> Dict[str, Dict[str, Any]]: | |||
| 112 | 115 | storing_columns[key].append(schema.column_name) | |
| 113 | 116 | ||
| 114 | 117 | index_schemas = index_schema.IndexSchema.where( | |
| 115 | - None, condition.equal_to('table_catalog', ''), | ||
| 116 | - condition.equal_to('table_schema', '')) | ||
| 118 | + condition.equal_to('table_catalog', ''), | ||
| 119 | + condition.equal_to('table_schema', ''), | ||
| 120 | + ) | ||
| 117 | 121 | indexes = collections.defaultdict(dict) | |
| 118 | 122 | for schema in index_schemas: | |
| 119 | 123 | key = (schema.table_name, schema.index_name) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -165,8 +165,7 @@ def _update_status(self, migration_id: str, new_status: bool) -> None: | |||
| 165 | 165 | 'migrated': new_status, | |
| 166 | 166 | 'update_time': datetime.datetime.utcnow(), | |
| 167 | 167 | }) | |
| 168 | - migration_status.MigrationStatus.save_batch( | ||
| 169 | - None, [new_model], force_write=True) | ||
| 168 | + migration_status.MigrationStatus.save_batch([new_model], force_write=True) | ||
| 170 | 169 | self._migration_status()[migration_id] = new_status | |
| 171 | 170 | ||
| 172 | 171 | def _validate_migrations(self) -> None: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -182,8 +182,9 @@ def validate(self) -> None: | |||
| 182 | 182 | ||
| 183 | 183 | # Verify no indices exist on the column we're trying to drop | |
| 184 | 184 | num_indexed_columns = index_column.IndexColumnSchema.count( | |
| 185 | - None, condition.equal_to('column_name', self._column), | ||
| 186 | - condition.equal_to('table_name', self._table)) | ||
| 185 | + condition.equal_to('column_name', self._column), | ||
| 186 | + condition.equal_to('table_name', self._table), | ||
| 187 | + ) | ||
| 187 | 188 | if num_indexed_columns > 0: | |
| 188 | 189 | raise error.SpannerError('Column {} is indexed'.format(self._column)) | |
| 189 | 190 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -184,6 +184,7 @@ def spanner_api(cls) -> api.SpannerApi: | |||
| 184 | 184 | @classmethod | |
| 185 | 185 | def all( | |
| 186 | 186 | cls: Type[T], | |
| 187 | + *, | ||
| 187 | 188 | transaction: Optional[spanner_transaction.Transaction] = None, | |
| 188 | 189 | ) -> List[T]: | |
| 189 | 190 | """Returns all objects of this type stored in Spanner. | |
@@ -206,17 +207,17 @@ def all( | |||
| 206 | 207 | @classmethod | |
| 207 | 208 | def count( | |
| 208 | 209 | cls, | |
| 209 | - transaction: Optional[spanner_transaction.Transaction], | ||
| 210 | 210 | *conditions: condition.Condition, | |
| 211 | + transaction: Optional[spanner_transaction.Transaction] = None, | ||
| 211 | 212 | ) -> int: | |
| 212 | 213 | """Returns the number of objects in Spanner that match the given conditions. | |
| 213 | 214 | ||
| 214 | 215 | Args: | |
| 215 | - transaction: The existing transaction to use, or None to start a new | ||
| 216 | - transaction | ||
| 217 | 216 | *conditions: Instances of subclasses of Condition that help specify which | |
| 218 | 217 | rows should be included in the count. The includes condition is not | |
| 219 | 218 | allowed here | |
| 219 | + transaction: The existing transaction to use, or None to start a new | ||
| 220 | + transaction | ||
| 220 | 221 | ||
| 221 | 222 | Returns: | |
| 222 | 223 | The integer result of the COUNT query | |
@@ -229,6 +230,7 @@ def count( | |||
| 229 | 230 | @classmethod | |
| 230 | 231 | def count_equal( | |
| 231 | 232 | cls, | |
| 233 | + *, | ||
| 232 | 234 | transaction: Optional[spanner_transaction.Transaction] = None, | |
| 233 | 235 | **constraints: Any, | |
| 234 | 236 | ) -> int: | |
@@ -253,11 +255,12 @@ def count_equal( | |||
| 253 | 255 | conditions.append(condition.in_list(column, value)) | |
| 254 | 256 | else: | |
| 255 | 257 | conditions.append(condition.equal_to(column, value)) | |
| 256 | - return cls.count(transaction, *conditions) | ||
| 258 | + return cls.count(*conditions, transaction=transaction) | ||
| 257 | 259 | ||
| 258 | 260 | @classmethod | |
| 259 | 261 | def find( | |
| 260 | 262 | cls: Type[T], | |
| 263 | + *, | ||
| 261 | 264 | transaction: Optional[spanner_transaction.Transaction] = None, | |
| 262 | 265 | **keys: Any, | |
| 263 | 266 | ) -> Optional[T]: | |
@@ -273,23 +276,24 @@ def find( | |||
| 273 | 276 | Returns: | |
| 274 | 277 | The requested object or None if no such object exists | |
| 275 | 278 | """ | |
| 276 | - resources = cls.find_multi(transaction, [keys]) | ||
| 279 | + resources = cls.find_multi([keys], transaction=transaction) | ||
| 277 | 280 | return resources[0] if resources else None | |
| 278 | 281 | ||
| 279 | 282 | @classmethod | |
| 280 | 283 | def find_multi( | |
| 281 | 284 | cls: Type[T], | |
| 282 | - transaction: Optional[spanner_transaction.Transaction], | ||
| 283 | 285 | keys: Iterable[Dict[str, Any]], | |
| 286 | + *, | ||
| 287 | + transaction: Optional[spanner_transaction.Transaction] = None, | ||
| 284 | 288 | ) -> List[T]: | |
| 285 | 289 | """Retrieves objects from Spanner based on the provided keys. | |
| 286 | 290 | ||
| 287 | 291 | Args: | |
| 288 | - transaction: The existing transaction to use, or None to start a new | ||
| 289 | - transaction | ||
| 290 | 292 | keys: An iterable of dictionaries, each dictionary representing the set of | |
| 291 | 293 | primary key values necessary to uniquely identify an object in this | |
| 292 | 294 | table. | |
| 295 | + transaction: The existing transaction to use, or None to start a new | ||
| 296 | + transaction | ||
| 293 | 297 | ||
| 294 | 298 | Returns: | |
| 295 | 299 | A list containing all requested objects that exist in the table (can be | |
@@ -307,16 +311,16 @@ def find_multi( | |||
| 307 | 311 | @classmethod | |
| 308 | 312 | def where( | |
| 309 | 313 | cls: Type[T], | |
| 310 | - transaction: Optional[spanner_transaction.Transaction], | ||
| 311 | 314 | *conditions: condition.Condition, | |
| 315 | + transaction: Optional[spanner_transaction.Transaction] = None, | ||
| 312 | 316 | ) -> List[T]: | |
| 313 | 317 | """Retrieves objects from Spanner based on the provided conditions. | |
| 314 | 318 | ||
| 315 | 319 | Args: | |
| 316 | - transaction: The existing transaction to use, or None to start a new | ||
| 317 | - transaction | ||
| 318 | 320 | *conditions: Instances of subclasses of Condition that help specify which | |
| 319 | 321 | objects should be retrieved | |
| 322 | + transaction: The existing transaction to use, or None to start a new | ||
| 323 | + transaction | ||
| 320 | 324 | ||
| 321 | 325 | Returns: | |
| 322 | 326 | A list containing all requested objects that exist in the table (can be | |
@@ -330,6 +334,7 @@ def where( | |||
| 330 | 334 | @classmethod | |
| 331 | 335 | def where_equal( | |
| 332 | 336 | cls: Type[T], | |
| 337 | + *, | ||
| 333 | 338 | transaction: Optional[spanner_transaction.Transaction] = None, | |
| 334 | 339 | **constraints: Any, | |
| 335 | 340 | ) -> List[T]: | |
@@ -352,7 +357,7 @@ def where_equal( | |||
| 352 | 357 | conditions.append(condition.in_list(column, value)) | |
| 353 | 358 | else: | |
| 354 | 359 | conditions.append(condition.equal_to(column, value)) | |
| 355 | - return cls.where(transaction, *conditions) | ||
| 360 | + return cls.where(*conditions, transaction=transaction) | ||
| 356 | 361 | ||
| 357 | 362 | @classmethod | |
| 358 | 363 | def _results_to_models( | |
@@ -378,6 +383,7 @@ def _execute_read( | |||
| 378 | 383 | @classmethod | |
| 379 | 384 | def create( | |
| 380 | 385 | cls, | |
| 386 | + *, | ||
| 381 | 387 | transaction: Optional[spanner_transaction.Transaction] = None, | |
| 382 | 388 | **kwargs: Any, | |
| 383 | 389 | ) -> None: | |
@@ -397,6 +403,7 @@ def create( | |||
| 397 | 403 | @classmethod | |
| 398 | 404 | def create_or_update( | |
| 399 | 405 | cls, | |
| 406 | + *, | ||
| 400 | 407 | transaction: Optional[spanner_transaction.Transaction] = None, | |
| 401 | 408 | **kwargs: Any, | |
| 402 | 409 | ) -> None: | |
@@ -418,15 +425,16 @@ def _delete_by_keyset( | |||
| 418 | 425 | @classmethod | |
| 419 | 426 | def delete_batch( | |
| 420 | 427 | cls: Type[T], | |
| 421 | - transaction: Optional[spanner_transaction.Transaction], | ||
| 422 | 428 | models: List[T], | |
| 429 | + *, | ||
| 430 | + transaction: Optional[spanner_transaction.Transaction] = None, | ||
| 423 | 431 | ) -> None: | |
| 424 | 432 | """Deletes rows from Spanner based on the provided models' primary keys. | |
| 425 | 433 | ||
| 426 | 434 | Args: | |
| 435 | + models: A list of models to be deleted from Spanner. | ||
| 427 | 436 | transaction: The existing transaction to use, or None to start a new | |
| 428 | 437 | transaction | |
| 429 | - models: A list of models to be deleted from Spanner. | ||
| 430 | 438 | """ | |
| 431 | 439 | key_list = [] | |
| 432 | 440 | for model in models: | |
@@ -439,6 +447,7 @@ def delete_batch( | |||
| 439 | 447 | @classmethod | |
| 440 | 448 | def delete_by_key( | |
| 441 | 449 | cls, | |
| 450 | + *, | ||
| 442 | 451 | transaction: Optional[spanner_transaction.Transaction] = None, | |
| 443 | 452 | **keys: Any, | |
| 444 | 453 | ) -> None: | |
@@ -460,20 +469,21 @@ def delete_by_key( | |||
| 460 | 469 | @classmethod | |
| 461 | 470 | def save_batch( | |
| 462 | 471 | cls: Type[T], | |
| 463 | - transaction: Optional[spanner_transaction.Transaction], | ||
| 464 | 472 | models: List[T], | |
| 473 | + *, | ||
| 474 | + transaction: Optional[spanner_transaction.Transaction] = None, | ||
| 465 | 475 | force_write: bool = False, | |
| 466 | 476 | ) -> None: | |
| 467 | 477 | """Writes rows to Spanner based on the provided model data. | |
| 468 | 478 | ||
| 469 | 479 | Args: | |
| 470 | - transaction: The existing transaction to use, or None to start a new | ||
| 471 | - transaction | ||
| 472 | 480 | models: A list of models to be written to Spanner. If the _persisted flag | |
| 473 | 481 | is set, by default we try to issue an UPDATE with values set for all | |
| 474 | 482 | columns in the table. Otherwise, we try to issue an INSERT for all | |
| 475 | 483 | columns in the table. If we try to INSERTa row that already exists (or | |
| 476 | 484 | update one that is missing), an exception will be thrown. | |
| 485 | + transaction: The existing transaction to use, or None to start a new | ||
| 486 | + transaction | ||
| 477 | 487 | force_write: If true, we use UPSERT instead of UPDATE/INSERT, so no | |
| 478 | 488 | exceptions are thrown based on the presence or absence of data in | |
| 479 | 489 | Spanner | |
@@ -495,6 +505,7 @@ def save_batch( | |||
| 495 | 505 | @classmethod | |
| 496 | 506 | def update( | |
| 497 | 507 | cls, | |
| 508 | + *, | ||
| 498 | 509 | transaction: Optional[spanner_transaction.Transaction] = None, | |
| 499 | 510 | **kwargs: Any, | |
| 500 | 511 | ) -> None: | |
@@ -596,7 +607,11 @@ def changes(self) -> Dict[str, Any]: | |||
| 596 | 607 | if values[key] != self.start_values.get(key) | |
| 597 | 608 | } | |
| 598 | 609 | ||
| 599 | - def delete(self, transaction: spanner_transaction.Transaction = None) -> None: | ||
| 610 | + def delete( | ||
| 611 | + self, | ||
| 612 | + *, | ||
| 613 | + transaction: Optional[spanner_transaction.Transaction] = None, | ||
| 614 | + ) -> None: | ||
| 600 | 615 | """Deletes this object from the Spanner database. | |
| 601 | 616 | ||
| 602 | 617 | Args: | |
@@ -625,7 +640,9 @@ def id(self) -> Dict[str, Any]: | |||
| 625 | 640 | ||
| 626 | 641 | def reload( | |
| 627 | 642 | self, | |
| 628 | - transaction: spanner_transaction.Transaction = None) -> Optional['Model']: | ||
| 643 | + *, | ||
| 644 | + transaction: Optional[spanner_transaction.Transaction] = None, | ||
| 645 | + ) -> Optional['Model']: | ||
| 629 | 646 | """Refreshes this object with information from Spanner. | |
| 630 | 647 | ||
| 631 | 648 | Args: | |
@@ -637,7 +654,7 @@ def reload( | |||
| 637 | 654 | in Spanner, or None if no information was found (object was deleted or | |
| 638 | 655 | never was persisted) | |
| 639 | 656 | """ | |
| 640 | - updated_object = self._metaclass.find(transaction, **self.id()) | ||
| 657 | + updated_object = self._metaclass.find(transaction=transaction, **self.id()) | ||
| 641 | 658 | if updated_object is None: | |
| 642 | 659 | return None | |
| 643 | 660 | start_values = {} | |
@@ -652,8 +669,11 @@ def reload( | |||
| 652 | 669 | self._persisted = True | |
| 653 | 670 | return self | |
| 654 | 671 | ||
| 655 | - def save(self, | ||
| 656 | - transaction: spanner_transaction.Transaction = None) -> 'Model': | ||
| 672 | + def save( | ||
| 673 | + self, | ||
| 674 | + *, | ||
| 675 | + transaction: Optional[spanner_transaction.Transaction] = None, | ||
| 676 | + ) -> 'Model': | ||
| 657 | 677 | """Persists this object to Spanner. | |
| 658 | 678 | ||
| 659 | 679 | Note: if the _persisted flag doesn't match whether this object is actually | |
@@ -671,8 +691,8 @@ def save(self, | |||
| 671 | 691 | changed_values = self.changes() | |
| 672 | 692 | if changed_values: | |
| 673 | 693 | changed_values.update(self.id()) | |
| 674 | - self._metaclass.update(transaction, **changed_values) | ||
| 694 | + self._metaclass.update(transaction=transaction, **changed_values) | ||
| 675 | 695 | else: | |
| 676 | - self._metaclass.create(transaction, **self.values) | ||
| 696 | + self._metaclass.create(transaction=transaction, **self.values) | ||
| 677 | 697 | self._persisted = True | |
| 678 | 698 | return self | |
| Back | FazBrowse Home | New Git URL |
0 commit comments