|
| 1 | +# ----------------------------------------------------------------------------- |
| 2 | +# Copyright (c) 2023, Oracle and/or its affiliates. |
| 3 | +# |
| 4 | +# This software is dual-licensed to you under the Universal Permissive License |
| 5 | +# (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License |
| 6 | +# 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose |
| 7 | +# either license. |
| 8 | +# |
| 9 | +# If you elect to accept the software under the Apache License, Version 2.0, |
| 10 | +# the following applies: |
| 11 | +# |
| 12 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 13 | +# you may not use this file except in compliance with the License. |
| 14 | +# You may obtain a copy of the License at |
| 15 | +# |
| 16 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 17 | +# |
| 18 | +# Unless required by applicable law or agreed to in writing, software |
| 19 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 20 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | +# See the License for the specific language governing permissions and |
| 22 | +# limitations under the License. |
| 23 | +# ----------------------------------------------------------------------------- |
| 24 | + |
| 25 | +# ----------------------------------------------------------------------------- |
| 26 | +# batch_errors_async.py |
| 27 | +# |
| 28 | +# An asynchronous version of batch_errors.py |
| 29 | +# |
| 30 | +# Demonstrates the use of the Oracle Database 12.1 feature that allows |
| 31 | +# cursor.executemany() to complete successfully, even if errors take |
| 32 | +# place during the execution of one or more of the individual |
| 33 | +# executions. The parameter "batcherrors" must be set to True in the |
| 34 | +# call to cursor.executemany() after which cursor.getbatcherrors() can |
| 35 | +# be called, which will return a list of error objects. |
| 36 | +# ----------------------------------------------------------------------------- |
| 37 | + |
| 38 | +import asyncio |
| 39 | + |
| 40 | +import oracledb |
| 41 | +import sample_env |
| 42 | + |
| 43 | + |
| 44 | +async def main(): |
| 45 | + connection = await oracledb.connect_async( |
| 46 | + user=sample_env.get_main_user(), |
| 47 | + password=sample_env.get_main_password(), |
| 48 | + dsn=sample_env.get_connect_string(), |
| 49 | + ) |
| 50 | + |
| 51 | + with connection.cursor() as cursor: |
| 52 | + # retrieve the number of rows in the table |
| 53 | + await cursor.execute("select count(*) from ChildTable") |
| 54 | + (count,) = await cursor.fetchone() |
| 55 | + print("Number of rows in child table:", int(count)) |
| 56 | + |
| 57 | + # define data to insert |
| 58 | + data_to_insert = [ |
| 59 | + (1016, 10, "Child B of Parent 10"), |
| 60 | + (1017, 10, "Child C of Parent 10"), |
| 61 | + (1018, 20, "Child D of Parent 20"), |
| 62 | + (1018, 20, "Child D of Parent 20"), # duplicate key |
| 63 | + (1019, 30, "Child C of Parent 30"), |
| 64 | + (1020, 30, "Child D of Parent 40"), |
| 65 | + (1021, 60, "Child A of Parent 60"), # parent does not exist |
| 66 | + (1022, 40, "Child F of Parent 40"), |
| 67 | + ] |
| 68 | + print("Number of rows to insert:", len(data_to_insert)) |
| 69 | + |
| 70 | + # old method: executemany() with data errors results in stoppage after |
| 71 | + # the first error takes place; the row count is updated to show how |
| 72 | + # many rows actually succeeded |
| 73 | + try: |
| 74 | + await cursor.executemany( |
| 75 | + "insert into ChildTable values (:1, :2, :3)", data_to_insert |
| 76 | + ) |
| 77 | + except oracledb.DatabaseError as e: |
| 78 | + (error,) = e.args |
| 79 | + print("Failure with error:", error.message) |
| 80 | + print("Number of rows successfully inserted:", cursor.rowcount) |
| 81 | + |
| 82 | + # demonstrate that the row count is accurate |
| 83 | + await cursor.execute("select count(*) from ChildTable") |
| 84 | + (count,) = await cursor.fetchone() |
| 85 | + print("Number of rows in child table after failed insert:", int(count)) |
| 86 | + |
| 87 | + # roll back so we can perform the same work using the new method |
| 88 | + await connection.rollback() |
| 89 | + |
| 90 | + # new method: executemany() with batch errors enabled (and array DML |
| 91 | + # row counts also enabled) results in no immediate error being raised |
| 92 | + await cursor.executemany( |
| 93 | + "insert into ChildTable values (:1, :2, :3)", |
| 94 | + data_to_insert, |
| 95 | + batcherrors=True, |
| 96 | + arraydmlrowcounts=True, |
| 97 | + ) |
| 98 | + |
| 99 | + # display the errors that have taken place |
| 100 | + errors = cursor.getbatcherrors() |
| 101 | + print("Number of rows with bad values:", len(errors)) |
| 102 | + for error in errors: |
| 103 | + print( |
| 104 | + "Error", error.message.rstrip(), "at row offset", error.offset |
| 105 | + ) |
| 106 | + |
| 107 | + # arraydmlrowcounts also shows rows with invalid data: they have a row |
| 108 | + # count of 0; otherwise 1 is shown |
| 109 | + row_counts = cursor.getarraydmlrowcounts() |
| 110 | + print("Array DML row counts:", row_counts) |
| 111 | + |
| 112 | + # demonstrate that all of the rows without errors have been |
| 113 | + # successfully inserted |
| 114 | + await cursor.execute("select count(*) from ChildTable") |
| 115 | + (count,) = await cursor.fetchone() |
| 116 | + print( |
| 117 | + "Number of rows in child table after insert with batcherrors " |
| 118 | + "enabled:", |
| 119 | + int(count), |
| 120 | + ) |
| 121 | + |
| 122 | + |
| 123 | +asyncio.run(main()) |
0 commit comments