Find all crash dumps that have common properties with the crash dump provided. Results can be paged to avoid consuming too much memory if the database is large. @see: L{find} @type crash: L{Crash} @param crash: Crash object to compare with
(self, crash, offset=None, limit=None)
| 887 | |
| 888 | @Transactional |
| 889 | def find_by_example(self, crash, offset=None, limit=None): |
| 890 | """ |
| 891 | Find all crash dumps that have common properties with the crash dump |
| 892 | provided. |
| 893 | |
| 894 | Results can be paged to avoid consuming too much memory if the database |
| 895 | is large. |
| 896 | |
| 897 | @see: L{find} |
| 898 | |
| 899 | @type crash: L{Crash} |
| 900 | @param crash: Crash object to compare with. Fields set to C{None} are |
| 901 | ignored, all other fields but the signature are used in the |
| 902 | comparison. |
| 903 | |
| 904 | To search for signature instead use the L{find} method. |
| 905 | |
| 906 | @type offset: int |
| 907 | @param offset: (Optional) Skip the first I{offset} results. |
| 908 | |
| 909 | @type limit: int |
| 910 | @param limit: (Optional) Return at most I{limit} results. |
| 911 | |
| 912 | @rtype: list(L{Crash}) |
| 913 | @return: List of similar crash dumps found. |
| 914 | """ |
| 915 | |
| 916 | # Validate the parameters. |
| 917 | if limit is not None and not limit: |
| 918 | warnings.warn("CrashDAO.find_by_example() was set a limit of 0 results, returning without executing a query.") |
| 919 | return [] |
| 920 | |
| 921 | # Build the query. |
| 922 | query = self._session.query(CrashDTO) |
| 923 | |
| 924 | # Order by row ID to get consistent results. |
| 925 | # Also some database engines require ordering when using offsets. |
| 926 | query = query.asc(CrashDTO.id) |
| 927 | |
| 928 | # Build a CrashDTO from the Crash object. |
| 929 | dto = CrashDTO(crash) |
| 930 | |
| 931 | # Filter all the fields in the crashes table that are present in the |
| 932 | # CrashDTO object and not set to None, except for the row ID. |
| 933 | for name, column in compat.iteritems(CrashDTO.__dict__): |
| 934 | if not name.startswith("__") and name not in ("id", "signature", "data"): |
| 935 | if isinstance(column, Column): |
| 936 | value = getattr(dto, name, None) |
| 937 | if value is not None: |
| 938 | query = query.filter(column == value) |
| 939 | |
| 940 | # Page the query. |
| 941 | if offset: |
| 942 | query = query.offset(offset) |
| 943 | if limit: |
| 944 | query = query.limit(limit) |
| 945 | |
| 946 | # Execute the SQL query and convert the results. |