1 | if ( !isInverse && isRowInsertEnabled() ) {↵ | | 1 | if ( !isInverse && isRowInsertEnabled() ) {↵
|
|
2 | if ( log.isDebugEnabled() ) {↵ | | 2 | if ( log.isDebugEnabled() ) {↵
|
3 | log.debug( ↵ | | 3 | log.debug( ↵
|
4 | "Inserting collection: " + ↵ | | 4 | "Inserting rows of collection: " + ↵
|
5 | MessageHelper.collectionInfoString( this, id, getFactory() ) ↵ | | 5 | MessageHelper.collectionInfoString( this, id, getFactory() ) ↵
|
6 | );↵ | | 6 | );↵
|
7 | }↵ | | 7 | }↵
|
|
8 | try {↵ | | 8 | try {↵
|
9 | //create all the new entries↵ | | 9 | //insert all the new entries↵
|
10 | Iterator entries = collection.entries(this);↵ | | 10 | ↵
|
11 | if ( entries.hasNext() ) {↵ | | |
|
12 | collection.preInsert( this );↵ | | |
|
13 | int i = 0;↵ | | |
|
14 | int count = 0;↵ | | |
|
15 | while ( entries.hasNext() ) {↵ | | |
|
|
16 | final Object entry = entries.next();↵ | | |
|
17 | if (↵ | | 11 | collection.preInsert( this );↵
|
18 | collection.entryExists( entry, i ) ) {↵ | | 12 | Iterator entries = collection.entr↵
|
19 | int offset = 1;↵ | | |
|
20 | PreparedStatement st = null;↵ | | |
|
21 | ↵ | | 13 | ies( this );↵
|
22 | Expectation expectation = Expectations.appropriateExpectation( getInsertCheckStyle() );↵ | | 14 | Expectation expectation = Expectations.appropriateExpectation( getInsertCheckStyle() );↵
|
23 | boolean callable = isInsertCallable();↵ | | 15 | boolean callable = isInsertCallable();↵
|
24 | boolean useBatch = expectation.canBeBatched();↵ | | 16 | boolean useBatch = expectation.canBeBatched();↵
|
25 | String sql = getSQLInsertRowString();↵ | | 17 | String sql = getSQLInsertRowString();↵
|
|
26 | if ( useBatch↵ | | 18 | int i = 0;↵
|
| | | 19 | int count = 0;↵
|
| | | 20 | while ( entries.hasNext() ) {↵
|
| | | 21 | int offset = 1;↵
|
| | | 22 | Object entry = entries.next();↵
|
| | | 23 | PreparedStatement st = null;↵
|
| | | 24 | if ( collection.needsInserting( entry, i, elementType ) ) {↵
|
|
| | | 25 | if ( useBatch ) {↵
|
27 | ) {↵ | | 26 | if ( st == null ) {↵
|
28 | if ( callable ) {↵ | | 27 | if ( callable ) {↵
|
29 | st = session.getBatcher().prepareBatchCallableStatement( sql );↵ | | 28 | st = session.getBatcher().prepareBatchCallableStatement( sql );↵
|
30 | }↵ | | 29 | }↵
|
31 | else {↵ | | 30 | else {↵
|
32 | st = session.getBatcher().prepareBatchStatement( sql );↵ | | 31 | st = session.getBatcher().prepareBatchStatement( sql );↵
|
33 | }↵ | | 32 | }↵
|
34 | }↵ | | 33 | }↵
|
35 | ↵ | | 34 | }↵
|
36 | else {↵ | | 35 | else {↵
|
37 | if ( callable ) {↵ | | 36 | if ( callable ) {↵
|
38 | st = session.getBatcher().prepareCallableStatement( sql );↵ | | 37 | st = session.getBatcher().prepareCallableStatement( sql );↵
|
39 | }↵ | | 38 | }↵
|
40 | else {↵ | | 39 | else {↵
|
41 | st = session.getBatcher().prepareStatement( sql );↵ | | 40 | st = session.getBatcher().prepareStatement( sql );↵
|
42 | }↵ | | 41 | }↵
|
43 | }↵ | | 42 | ↵
|
| | | 43 | }↵
|
|
44 | try {↵ | | 44 | try {↵
|
45 | offset+= expectation.prepare( st );↵ | | 45 | offset += expectation.prepare( st );↵
|
|
46 | //TODO: copy/paste from insertRows()↵ | | 46 | //TODO: copy/paste from recreate()↵
|
47 | int loc = writeKey( st, id, offset, session );↵ | | 47 | offset = writeKey( st, id, offset, session );↵
|
48 | if ( hasIdentifier ) {↵ | | 48 | if ( hasIdentifier ) {↵
|
49 | loc = writeIdentifier( st, collection.getIdentifier(entry, i), loc, session );↵ | | 49 | offset = writeIdentifier( st, collection.getIdentifier(entry, i), offset, session );↵
|
50 | }↵ | | 50 | }↵
|
51 | if ( hasIndex /*&& !indexIsFormula*/ ) {↵ | | 51 | if ( hasIndex /*&& !indexIsFormula*/ ) {↵
|
52 | loc = writeIndex( st, collection.getIndex(entry, i, this), loc, session );↵ | | 52 | offset = writeIndex( st, collection.getIndex(entry, i, this), offset, session );↵
|
53 | }↵ | | 53 | }↵
|
54 | loc = writeElement(st, collection.getElement(entry), loc, session );↵ | | 54 | writeElement(st, collection.getElement(entry), offset, session );↵
|
|
55 | if ( useBatch ) {↵ | | 55 | if ( useBatch ) {↵
|
56 | session.getBatcher().addToBatch( expectation );↵ | | 56 | session.getBatcher().addToBatch( expectation );↵
|
57 | }↵ | | 57 | }↵
|
58 | else {↵ | | 58 | else {↵
|
59 | expectation.verifyOutcome( st.executeUpdate(), st, -1 );↵ | | 59 | expectation.verifyOutcome( st.executeUpdate(), st, -1 );↵
|
60 | }↵ | | 60 | }↵
|
|
61 | collection.afterRowInsert( this, entry, i );↵ | | 61 | collection.afterRowInsert( this, entry, i );↵
|
62 | count++;↵ | | 62 | count++;↵
|
63 | }↵ | | 63 | }↵
|
64 | catch ( SQLException sqle ) {↵ | | 64 | catch ( SQLException sqle ) {↵
|
65 | if ( useBatch ) {↵ | | 65 | if ( useBatch ) {↵
|
66 | session.getBatcher().abortBatch( sqle );↵ | | 66 | session.getBatcher().abortBatch( sqle );↵
|
67 | }↵ | | 67 | }↵
|
68 | throw sqle;↵ | | 68 | throw sqle;↵
|
69 | }↵ | | 69 | }↵
|
70 | finally {↵ | | 70 | finally {↵
|
71 | if ( !useBatch ) {↵ | | 71 | if ( !useBatch ) {↵
|
72 | session.getBatcher().closeStatement( st );↵ | | 72 | session.getBatcher().closeStatement( st );↵
|
73 | }↵ | | 73 | }↵
|
74 | }↵ | | 74 | ↵
|
| | | 75 | }↵
|
75 | }↵ | | 76 | }↵
|
76 | i++;↵ | | 77 | i++;↵
|
77 | }↵ | | 78 | }↵
|
|
78 | if ( log.isDebugEnabled() ) {↵ | | 79 | if ( log.isDebugEnabled() ) {↵
|
79 | log.debug( "done inserting collection: " + count + " rows inserted" );↵ | | 80 | log.debug( "done inserting rows: " + count + " inserted" );↵
|
80 | }↵ | | 81 | ↵
|
|
81 | }↵ | | |
|
82 | else {↵ | | |
|
83 | if ( log.isDebugEnabled() ) {↵ | | |
|
84 | log.debug( "collection was empty" );↵ | | |
|
85 | }↵ | | |
|
86 | }↵ | | 82 | }↵
|
87 | }↵ | | 83 | }↵
|
88 | catch ( SQLException sqle ) {↵ | | 84 | catch ( SQLException sqle ) {↵
|
89 | throw JDBCExceptionHelper.convert(↵ | | 85 | throw JDBCExceptionHelper.convert(↵
|
90 | sqlExceptionConverter,↵ | | 86 | sqlExceptionConverter,↵
|
91 | sqle,↵ | | 87 | sqle,↵
|
92 | "could not insert collection: " + ↵ | | 88 | "could not insert collection rows: " + ↵
|
93 | MessageHelper.collectionInfoString( this, id, getFactory() ),↵ | | 89 | MessageHelper.collectionInfoString( this, id, getFactory() ),↵
|
94 | getSQLInsertRowString()↵ | | 90 | getSQLInsertRowString()↵
|
95 | );↵ | | 91 | );↵
|
96 | }↵ | | 92 | }↵
|
|
97 | } | | 93 | }
|