Relation¶
API Status
API documentation is auto-generated from docstrings. Ensure docstrings are comprehensive.
Overview¶
Method Categories¶
Relation methods are divided into two categories:
Query Builders (Lazy)¶
Return modified relation objects without executing SQL:
- ho_order_by()
, ho_limit()
, ho_offset()
- Set operations: &
, |
, -
, ^
- Foreign key navigation: relation_fk()
, relation_rfk()
Query Executors (Eager)¶
Execute SQL immediately and return results:
- ho_select(*fields)
→ Generator
- ho_count()
→ int
- ho_get()
→ dict
- ho_is_empty()
→ bool
- ho_insert()
, ho_update()
, ho_delete()
→ dict
No Chaining After Execution
Conceptual Background
This builder/executor pattern is core to halfORM's design. Learn more in halfORM Fundamentals.
Reference¶
relation
¶
This module is used by the model <#module-half_orm.model>
_ module
to generate the classes that manipulate the data in your database
with the Model.get_relation_class <#half_orm.model.Model.get_relation_class>
_
method.
Example
from half_orm.model import Model model = Model('halftest') class Person(model.get_relation_class('actor.person')): # your code goes here
Main methods provided by the class Relation: - ho_insert: inserts a tuple into the pg table. - ho_select: returns a generator of the elements of the set defined by the constraint on the Relation object. The elements are dictionaries with the keys corresponding to the selected columns names in the relation. The result is affected by the methods: ho_distinct, ho_order_by, ho_limit and ho_offset (see below). - ho_update: updates the set defined by the constraint on the Relation object with the values passed as arguments. - ho_delete: deletes from the relation the set of elements defined by the constraint on the Relation object. - ho_get: returns the unique element defined by the constraint on the Relation object. the element returned if of the type of the Relation object.
The following methods can be chained on the object before a select.
- ho_distinct: ensures that there are no duplicates on the select result.
- ho_order_by: sets the order of the select result.
- ho_limit: limits the number of elements returned by the select method.
- ho_offset: sets the offset for the select method.
Classes¶
DC_Relation
dataclass
¶
Source code in half_orm/relation.py
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 |
|
Functions¶
ho_count(limit=0)
¶
ho_delete(*args, delete_all=False)
¶
removes all elements from the set that correspond to the constraint.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
[Optional]
|
|
()
|
ho_distinct()
¶
ho_get(*args)
¶
The get method allows you to fetch a singleton from the database. It garantees that the constraint references one and only one tuple.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args
|
List[str]
|
list of fields names. If ommitted, all the values of the row retreived from the database are set for the self object. Otherwise, only the values listed in the |
()
|
Returns:
Name | Type | Description |
---|---|---|
Relation |
Relation
|
the object retreived from the database. |
Raises:
Type | Description |
---|---|
ExpectedOneError
|
an exception is raised if no or more than one element is found. |
Example
gaston = Person(last_name='Lagaffe', first_name='Gaston').ho_get() type(gaston) is Person True gaston.id (int4) NOT NULL (id = 1772) str(gaston.id) '1772' gaston.id.value 1772
Source code in half_orm/relation.py
ho_insert(*args)
¶
Insert a new tuple into the Relation.
Returns:
Name | Type | Description |
---|---|---|
Dict |
Dict
|
A dictionary containing the data inserted. |
Example
gaston = Person(last_name='La', first_name='Ga', birth_date='1970-01-01').ho_insert() print(gaston)
Note
It is not possible to insert more than one row with the ho_insert method
Source code in half_orm/relation.py
ho_is_empty()
¶
ho_is_set()
¶
Return True if one field at least is set or if self has been constrained by at least one of its foreign keys or self is the result of a combination of Relations (using set operators).
ho_limit(_limit_)
¶
ho_offset(_offset_)
¶
ho_order_by(_order_)
¶
Sets the SQL order by
according to the "order" string passed
Example
personnes.ho_order_by("field1, field2 desc, field3, field4 desc")
ho_select(*args)
¶
Gets the set of values correponding to the constraint attached to self. This method is a generator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
List[str]
|
the fields names of the returned attributes. If omitted, all the fields are returned. |
()
|
Yields:
Type | Description |
---|---|
[Dict]
|
the result of the query as a list of dictionaries. |
Example
for person in Person(last_name=('like', 'La%')).ho_select('id'): print(person)
Source code in half_orm/relation.py
ho_unaccent(*fields_names)
¶
ho_update(*args, update_all=False, **kwargs)
¶
Updates the elements defined by self.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
the values to be updated {[field name:value]} |
{}
|
|
*args
|
[Optional]
|
the list of columns names to return in the dictionary list for the updated elements. If args is ('*', ), returns all the columns values. |
()
|
update_all
|
a boolean that must be set to True if there is no constraint on |
False
|
Source code in half_orm/relation.py
Relation
¶
Used as a base class for the classes generated by
Model.get_relation_class <#half_orm.model.Model.get_relation_class>
_.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
the arguments names must correspond to the columns names of the relation. |
{}
|
Raises:
Type | Description |
---|---|
UnknownAttributeError
|
If the name of an argument doesn't match a column name in the relation considered. |
Examples:
You can generate a class for any relation in your database: >>> from half_orm.model import Model >>> model = Model('halftest') >>> class Person(model.get_relation_class('actor.person')): >>> # your code
To define a set of data in your relation at instantiation: >>> gaston = Person(last_name='Lagaffe', first_name='Gaston') >>> all_names_starting_with_la = Person(last_name=('ilike', 'la%'))
Or to constrain an instantiated object via its Fields <#half_orm.field.Field>
_:
>>> person = Person()
>>> person.birth_date = ('>', '1970-01-01')
Raises an UnknownAttributeError <#half_orm.relation_errors.UnknownAttributeError>
_:
>>> Person(lost_name='Lagaffe')
[...]UnknownAttributeError: ERROR! Unknown attribute: {'lost_name'}.
Source code in half_orm/relation.py
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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 |
|
Attributes¶
ho_id
property
¶
Return the _ho_id_cast or the id of the relation.
ho_only
property
writable
¶
Returns the value of self._ho_only
Functions¶
__enter__()
¶
Context management entry
Returns self in a transaction context.
Example usage: with relation as rel: rel.ho_update(col=new_val)
Equivalent to (in a transaction context): rel = relation.ho_select() for elt in rel: new_elt = relation(**elt) new_elt.ho_update(col=new_val)
Source code in half_orm/relation.py
__exit__(*__)
¶
__get_from(orig_rel=None, deja_vu=None)
¶
Constructs the _ho_sql_query and gets the _ho_sql_values for self.
Source code in half_orm/relation.py
__get_set_fields()
¶
__prep_query(query_template, *args)
¶
Prepare the SQL query to be executed.
Source code in half_orm/relation.py
__set__op__(operator=None, right=None)
¶
Si l'opérateur du self est déjà défini, il faut aller modifier l'opérateur du right ??? On crée un nouvel objet sans contrainte et on a left et right et opérateur
Source code in half_orm/relation.py
__setattr__(key, value)
¶
Sets an attribute as long as _ho_isfrozen is False
The foreign keys properties are not detected by hasattr
hence the line _ = self.__dict__[key]
to double check if
the attribute is really present.
Source code in half_orm/relation.py
__to_dict_val_comp()
¶
Returns a dictionary containing the values and comparators of the fields that are set.
__update_args(**kwargs)
¶
Returns the what, where an values for the update query.
Source code in half_orm/relation.py
__walk_op(rel_id_, out=None, _fields_=None)
¶
Walk the set operators tree and return a list of SQL where representation of the query with a list of the fields of the query.
Source code in half_orm/relation.py
__what()
¶
Returns the constrained fields and foreign keys.
Source code in half_orm/relation.py
__where_args(*args)
¶
Returns the what, where and values needed to construct the queries.
Source code in half_orm/relation.py
ho_cast(qrn)
¶
Cast a relation into another relation.
TODO: check that qrn inherits self (or is inherited by self)?
Source code in half_orm/relation.py
ho_count(*args)
¶
Returns the number of tuples matching the intention in the relation.
Source code in half_orm/relation.py
ho_delete(*args, delete_all=False)
¶
Removes a set of tuples from the relation. To empty the relation, delete_all must be set to True.
Source code in half_orm/relation.py
ho_description()
classmethod
¶
Returns the description (comment) of the relation
Source code in half_orm/relation.py
ho_dict()
¶
Returns a dictionary containing only the values of the fields that are set.
ho_distinct(dist=True)
¶
Set distinct in SQL select request.
Source code in half_orm/relation.py
ho_freeze()
¶
ho_get(*args)
¶
The get method allows you to fetch a singleton from the database. It garantees that the constraint references one and only one tuple.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args
|
List[str]
|
list of fields names. If ommitted, all the values of the row retreived from the database are set for the self object. Otherwise, only the values listed in the |
()
|
Returns:
Name | Type | Description |
---|---|---|
Relation |
Relation
|
the object retreived from the database. |
Raises:
Type | Description |
---|---|
ExpectedOneError
|
an exception is raised if no or more than one element is found. |
Example
gaston = Person(last_name='Lagaffe', first_name='Gaston').ho_get() type(gaston) is Person True gaston.id (int4) NOT NULL (id = 1772) str(gaston.id) '1772' gaston.id.value 1772
Source code in half_orm/relation.py
ho_insert(*args)
¶
Insert a new tuple into the Relation.
Returns:
Type | Description |
---|---|
[dict]
|
[dict]: A singleton containing the data inserted. |
Example
gaston = Person(last_name='La', first_name='Ga', birth_date='1970-01-01').ho_insert() print(gaston)
Note
It is not possible to insert more than one row with the insert method
Source code in half_orm/relation.py
ho_is_empty()
¶
ho_is_set()
¶
Return True if one field at least is set or if self has been constrained by at least one of its foreign keys or self is the result of a combination of Relations (using set operators).
Source code in half_orm/relation.py
ho_limit(_limit_)
¶
Set limit for the next SQL select request.
ho_mogrify()
¶
ho_offset(_offset_)
¶
ho_order_by(_order_)
¶
Set SQL order by according to the "order" string passed
@order string example : "field1, field2 desc, field3, field4 desc"
ho_select(*args)
¶
Gets the set of values correponding to the constraint attached to the object. This method is a generator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
the fields names of the returned attributes. If omitted, all the fields are returned. |
()
|
Yields:
Type | Description |
---|---|
the result of the query as a dictionary. |
Example
for person in Person(last_name=('like', 'La%')).ho_select('id'): print(person)
Source code in half_orm/relation.py
ho_unaccent(*fields_names)
¶
Sets unaccent for each field listed in fields_names
Source code in half_orm/relation.py
ho_unfreeze()
¶
ho_update(*args, update_all=False, **kwargs)
¶
kwargs represents the values to be updated {[field name:value]} The object self must be set unless update_all is True. The constraints of self are updated with kwargs.
Source code in half_orm/relation.py
Functions¶
singleton(fct)
¶
Decorator. Enforces the relation to define a singleton.
_ho_is_singleton is set by Relation.get. _ho_is_singleton is unset as soon as a Field is set.
Source code in half_orm/relation.py
transaction(fct)
¶
Decorator. Enforces every SQL insert, update or delete operation called within a Relation method to be executed in a transaction.
Usage
from relation import transaction class Person(model.get_relation_class(actor.person)): [...] @transaction def insert_many(self, **data): for d_pers in **data: self(**d_pers).ho_insert() [...]
Pers().insert_many([{...}, {...}])