2424import org .apache .cayenne .access .jdbc .RSColumn ;
2525import org .apache .cayenne .access .translator .TranslatedBatch ;
2626import org .apache .cayenne .access .translator .TranslatedSelect ;
27+ import org .apache .cayenne .access .translator .TranslatedStatement ;
2728import org .apache .cayenne .configuration .Constants ;
2829import org .apache .cayenne .configuration .RuntimeProperties ;
2930import org .apache .cayenne .map .DbAttribute ;
@@ -55,8 +56,16 @@ private static Slf4jSqlLogger loggerWithThreshold(int threshold) {
5556 return new Slf4jSqlLogger (props );
5657 }
5758
58- private static PSParameter <?> ps (String name , Object value ) {
59- return new PSParameter <>(value , 1 , Types .INTEGER , 0 , null , new DbAttribute (name ));
59+ private static String line (Slf4jSqlLogger logger , TranslatedStatement statement , String label , int count ) {
60+ StringBuilder buffer = new StringBuilder ();
61+ logger .appendStatementLine (buffer , statement , label , count );
62+ return buffer .toString ();
63+ }
64+
65+ private static String errorLine (Slf4jSqlLogger logger , TranslatedStatement statement , Throwable error , long durationMillis ) {
66+ StringBuilder buffer = new StringBuilder ();
67+ logger .appendErrorLine (buffer , statement , error , durationMillis );
68+ return buffer .toString ();
6069 }
6170
6271 // a single-placeholder batch whose id values are 0..rows-1, so each logged row is identifiable by its value
@@ -73,32 +82,34 @@ private static TranslatedBatch idBatch(int rows) {
7382 public void selectLineWithSingleBinding () {
7483 TranslatedSelect select = new TranslatedSelect (
7584 "SELECT t0.id FROM my_table t0 WHERE t0.user_id = ?" ,
76- new PSParameter <?>[]{ps ( "user_id" , 15 )},
85+ new PSParameter <?>[]{new PSParameter <>( 15 , 1 , Types . INTEGER , 0 , null , new DbAttribute ( "user_id" ) )},
7786 new RSColumn [0 ], false , false );
7887
79- assertEquals ("SELECT t0.id FROM my_table t0 WHERE t0.user_id = ? [bind:[user_id:15]] [selected:1]" ,
80- logger .buildStatementLine (select , "selected:" , 1 ));
88+ assertEquals (
89+ "SELECT t0.id FROM my_table t0 WHERE t0.user_id = ? | bind:[user_id:15] selected:1" ,
90+ line (logger , select , "selected:" , 1 ));
8191 }
8292
8393 @ Test
8494 public void errorLineCarriesBindingsMessageAndDuration () {
8595 TranslatedSelect select = new TranslatedSelect (
8696 "SELECT t0.id FROM my_table t0 WHERE t0.user_id = ?" ,
87- new PSParameter <?>[]{ps ( "user_id" , 15 )},
97+ new PSParameter <?>[]{new PSParameter <>( 15 , 1 , Types . INTEGER , 0 , null , new DbAttribute ( "user_id" ) )},
8898 new RSColumn [0 ], false , false );
8999
90- assertEquals ("SELECT t0.id FROM my_table t0 WHERE t0.user_id = ? [bind:[user_id:15]] "
91- + "[ time_ms:1000] [*** error: bad column] " ,
92- logger . buildErrorLine ( select , new RuntimeException ("bad column" ), 1000 ));
100+ assertEquals (
101+ "SELECT t0.id FROM my_table t0 WHERE t0.user_id = ? | bind:[user_id:15] time_ms:1000 error: bad column" ,
102+ errorLine ( logger , select , new RuntimeException ("bad column" ), 1000 ));
93103 }
94104
95105 @ Test
96106 public void selectLineWithoutBindings () {
97107 TranslatedSelect select = new TranslatedSelect (
98108 "SELECT t0.id FROM my_table t0" , new PSParameter <?>[0 ], new RSColumn [0 ], false , false );
99109
100- assertEquals ("SELECT t0.id FROM my_table t0 [selected:0]" ,
101- logger .buildStatementLine (select , "selected:" , 0 ));
110+ assertEquals (
111+ "SELECT t0.id FROM my_table t0 | selected:0" ,
112+ line (logger , select , "selected:" , 0 ));
102113 }
103114
104115 @ Test
@@ -112,9 +123,9 @@ public void batchLineSplitsHeadAndTailAroundElision() {
112123 TranslatedBatch batch = new TranslatedBatch (
113124 "INSERT INTO table1(id, name) VALUES(?, ?)" , new PSBatchParameter []{id , name });
114125
115- assertEquals ("INSERT INTO table1(id, name) VALUES(?, ?) "
116- + "[ bind:[id:3,name:'n3'][id:1,name:'n1']..2..[id:2,name:'n2']] [ updated:5] " ,
117- logger . buildStatementLine ( batch , "updated:" , 5 ));
126+ assertEquals (
127+ "INSERT INTO table1(id, name) VALUES(?, ?) | bind:[id:3,name:'n3'][id:1,name:'n1']..2..[id:2,name:'n2'] updated:5" ,
128+ line ( logger , batch , "updated:" , 5 ));
118129 }
119130
120131 @ Test
@@ -127,58 +138,57 @@ public void singleRowBatchHasNoDoubleBrackets() {
127138 TranslatedBatch batch = new TranslatedBatch (
128139 "INSERT INTO ARTIST(ARTIST_ID, ARTIST_NAME) VALUES(?, ?)" , new PSBatchParameter []{id , name });
129140
130- assertEquals ("INSERT INTO ARTIST(ARTIST_ID, ARTIST_NAME) VALUES(?, ?) [bind:[ARTIST_ID:200,ARTIST_NAME:'Test']] [updated:1]" ,
131- logger .buildStatementLine (batch , "updated:" , 1 ));
141+ assertEquals (
142+ "INSERT INTO ARTIST(ARTIST_ID, ARTIST_NAME) VALUES(?, ?) | bind:[ARTIST_ID:200,ARTIST_NAME:'Test'] updated:1" ,
143+ line (logger , batch , "updated:" , 1 ));
132144 }
133145
134146 @ Test
135147 public void batchLineShowsAllRowsBelowThreshold () {
136148 PSBatchParameter id = new PSBatchParameter (
137149 new Object []{3 , 2 }, 1 , Types .INTEGER , 0 , new DbAttribute ("id" ));
138150
139- TranslatedBatch batch = new TranslatedBatch (
140- "INSERT INTO table1(id) VALUES(?)" , new PSBatchParameter []{id });
141-
142- assertEquals ("INSERT INTO table1(id) VALUES(?) [bind:[id:3][id:2]] [updated:2]" ,
143- logger .buildStatementLine (batch , "updated:" , 2 ));
151+ TranslatedBatch batch = new TranslatedBatch ("INSERT INTO table1(id) VALUES(?)" , new PSBatchParameter []{id });
152+ assertEquals ("INSERT INTO table1(id) VALUES(?) | bind:[id:3][id:2] updated:2" ,
153+ line (logger , batch , "updated:" , 2 ));
144154 }
145155
146156 @ Test
147157 public void batchLineEvenThresholdSplitsEvenly () {
148158 // even threshold 4 -> head 2, tail 2; 10 rows -> middle 6 elided
149- assertEquals ("INSERT INTO t(id) VALUES(?) [ bind:[id:0][id:1]..6..[id:8][id:9]] [ updated:10] " ,
150- loggerWithThreshold (4 ). buildStatementLine ( idBatch (10 ), "updated:" , 10 ));
159+ assertEquals ("INSERT INTO t(id) VALUES(?) | bind:[id:0][id:1]..6..[id:8][id:9] updated:10" ,
160+ line ( loggerWithThreshold (4 ), idBatch (10 ), "updated:" , 10 ));
151161 }
152162
153163 @ Test
154164 public void batchLineShowsAllRowsAtThreshold () {
155165 // exactly threshold rows -> nothing elided
156- assertEquals ("INSERT INTO t(id) VALUES(?) [ bind:[id:0][id:1][id:2][id:3]] [ updated:4] " ,
157- loggerWithThreshold (4 ). buildStatementLine ( idBatch (4 ), "updated:" , 4 ));
166+ assertEquals ("INSERT INTO t(id) VALUES(?) | bind:[id:0][id:1][id:2][id:3] updated:4" ,
167+ line ( loggerWithThreshold (4 ), idBatch (4 ), "updated:" , 4 ));
158168 }
159169
160170 @ Test
161171 public void batchLineElidesExactlyOneAboveThreshold () {
162172 // one row above threshold 4 -> head 2, tail 2, a single row elided
163- assertEquals ("INSERT INTO t(id) VALUES(?) [ bind:[id:0][id:1]..1..[id:3][id:4]] [ updated:5] " ,
164- loggerWithThreshold (4 ). buildStatementLine ( idBatch (5 ), "updated:" , 5 ));
173+ assertEquals ("INSERT INTO t(id) VALUES(?) | bind:[id:0][id:1]..1..[id:3][id:4] updated:5" ,
174+ line ( loggerWithThreshold (4 ), idBatch (5 ), "updated:" , 5 ));
165175 }
166176
167177 @ Test
168178 public void batchLineThresholdOfOneOrTwoClampsToTwo () {
169179 // threshold 1 and 2 both clamp to 2 -> head 1, tail 1
170- assertEquals ("INSERT INTO t(id) VALUES(?) [ bind:[id:0]..3..[id:4]] [ updated:5] " ,
171- loggerWithThreshold (1 ). buildStatementLine ( idBatch (5 ), "updated:" , 5 ));
172- assertEquals ("INSERT INTO t(id) VALUES(?) [ bind:[id:0]..3..[id:4]] [ updated:5] " ,
173- loggerWithThreshold (2 ). buildStatementLine ( idBatch (5 ), "updated:" , 5 ));
180+ assertEquals ("INSERT INTO t(id) VALUES(?) | bind:[id:0]..3..[id:4] updated:5" ,
181+ line ( loggerWithThreshold (1 ), idBatch (5 ), "updated:" , 5 ));
182+ assertEquals ("INSERT INTO t(id) VALUES(?) | bind:[id:0]..3..[id:4] updated:5" ,
183+ line ( loggerWithThreshold (2 ), idBatch (5 ), "updated:" , 5 ));
174184 }
175185
176186 @ Test
177187 public void batchLineNonPositiveThresholdDisablesTruncation () {
178188 // threshold 0 or negative -> no truncation, every row logged
179- assertEquals ("INSERT INTO t(id) VALUES(?) [ bind:[id:0][id:1][id:2][id:3][id:4]] [ updated:5] " ,
180- loggerWithThreshold (0 ). buildStatementLine ( idBatch (5 ), "updated:" , 5 ));
181- assertEquals ("INSERT INTO t(id) VALUES(?) [ bind:[id:0][id:1][id:2][id:3][id:4]] [ updated:5] " ,
182- loggerWithThreshold (-1 ). buildStatementLine ( idBatch (5 ), "updated:" , 5 ));
189+ assertEquals ("INSERT INTO t(id) VALUES(?) | bind:[id:0][id:1][id:2][id:3][id:4] updated:5" ,
190+ line ( loggerWithThreshold (0 ), idBatch (5 ), "updated:" , 5 ));
191+ assertEquals ("INSERT INTO t(id) VALUES(?) | bind:[id:0][id:1][id:2][id:3][id:4] updated:5" ,
192+ line ( loggerWithThreshold (-1 ), idBatch (5 ), "updated:" , 5 ));
183193 }
184194}
0 commit comments