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