2626@ SuppressWarnings ("SqlNoDataSourceInspection" )
2727public class SqlOnJsonTest {
2828
29+ private final SqlOnJson sqlOnJson = new SqlOnJson ();
30+
2931 @ Test
3032 public void representEmptyJsonAsEmptyDb () throws Exception {
31- try (Connection c = SqlOnJson .convertPlain ("" )) {
33+ try (Connection c = sqlOnJson .convertPlain ("" )) {
3234 ResultSet rs = c .getMetaData ().getTables (null , null , "a" , null );
3335 Assert .assertFalse (rs .next ());
3436 }
3537 }
3638
3739 @ Test
3840 public void representObjectWithEmptyArrayPropertyAsEmptyDb () throws Exception {
39- try (Connection c = SqlOnJson .convertPlain ("{a:[]}" )) {
41+ try (Connection c = sqlOnJson .convertPlain ("{a:[]}" )) {
4042 ResultSet rs = c .getMetaData ().getTables (null , null , "a" , null );
4143 Assert .assertFalse (rs .next ());
4244 }
4345 }
4446
4547 @ Test
4648 public void representObjectWithArrayPropertyAsTableInDb () throws Exception {
47- try (Connection c = SqlOnJson .convertPlain ("{a:[{id:12000,name:\" super\" },{id:90,name:\" remta\" }]}" )) {
49+ try (Connection c = sqlOnJson .convertPlain ("{a:[{id:12000,name:\" super\" },{id:90,name:\" remta\" }]}" )) {
4850 ResultSet rs = c .prepareStatement ("select * from a" ).executeQuery ();
4951 rs .next ();
5052 Assert .assertEquals (12000 , rs .getLong ("id" ));
@@ -57,7 +59,7 @@ public void representObjectWithArrayPropertyAsTableInDb() throws Exception {
5759
5860 @ Test
5961 public void ignoreNonAlphaNumberAndNonAlphaFirstCharacters () throws Exception {
60- try (Connection c = SqlOnJson .convertPlain ("{\" _AmO_(Nit)\" :[{\" _i-d,()rumbA\" :12000,_12:90}]}" )) {
62+ try (Connection c = sqlOnJson .convertPlain ("{\" _AmO_(Nit)\" :[{\" _i-d,()rumbA\" :12000,_12:90}]}" )) {
6163 ResultSet rs = c .prepareStatement ("select * from iamo_nit" ).executeQuery ();
6264 rs .next ();
6365 Assert .assertEquals (12000 , rs .getLong ("iidrumba" ));
@@ -68,7 +70,7 @@ public void ignoreNonAlphaNumberAndNonAlphaFirstCharacters() throws Exception {
6870 @ Test
6971 public void support8kOfCharactersForStringFields () throws Exception {
7072 String string8k = StringUtils .repeat ('z' , 8 * 1000 );
71- try (Connection c = SqlOnJson .convertPlain ("{longs:[{str:\" " + string8k + "\" }]}" )) {
73+ try (Connection c = sqlOnJson .convertPlain ("{longs:[{str:\" " + string8k + "\" }]}" )) {
7274 ResultSet rs = c .prepareStatement ("select * from longs" ).executeQuery ();
7375 rs .next ();
7476 Assert .assertEquals (string8k , rs .getString ("str" ));
@@ -77,7 +79,7 @@ public void support8kOfCharactersForStringFields() throws Exception {
7779
7880 @ Test
7981 public void supportCaseWhenNonFirstObjectHasMoreProperties () throws Exception {
80- try (Connection c = SqlOnJson .convertPlain ("{nosql:[{id:12},{id:15,mid:90}]}" )) {
82+ try (Connection c = sqlOnJson .convertPlain ("{nosql:[{id:12},{id:15,mid:90}]}" )) {
8183 ResultSet rs = c .prepareStatement ("select * from nosql" ).executeQuery ();
8284 rs .next ();
8385 Assert .assertEquals (12 , rs .getLong ("id" ));
@@ -90,8 +92,8 @@ public void supportCaseWhenNonFirstObjectHasMoreProperties() throws Exception {
9092 @ Test
9193 public void supportParallelWorkWithTwoDb () throws Exception {
9294 try (
93- Connection c1 = SqlOnJson .convertPlain ("{nosql:[{id:12},{id:15,mid:90}]}" );
94- Connection c2 = SqlOnJson .convertPlain ("{nosql:[{a:1}]}" );
95+ Connection c1 = sqlOnJson .convertPlain ("{nosql:[{id:12},{id:15,mid:90}]}" );
96+ Connection c2 = sqlOnJson .convertPlain ("{nosql:[{a:1}]}" );
9597 ) {
9698 ResultSet rs1 = c1 .prepareStatement ("select * from nosql" ).executeQuery ();
9799 Assert .assertTrue (rs1 .next ());
@@ -102,7 +104,7 @@ public void supportParallelWorkWithTwoDb() throws Exception {
102104
103105 @ Test
104106 public void representNumberPropertyAsLong () throws Exception {
105- try (Connection c = SqlOnJson .convertPlain ("{a:[{o:" + Long .MAX_VALUE + "},{o:" + Long .MIN_VALUE + "}]}" )) {
107+ try (Connection c = sqlOnJson .convertPlain ("{a:[{o:" + Long .MAX_VALUE + "},{o:" + Long .MIN_VALUE + "}]}" )) {
106108 ResultSet rs = c .prepareStatement ("select * from a" ).executeQuery ();
107109 Assert .assertEquals ("BIGINT" , rs .getMetaData ().getColumnTypeName (1 ));
108110 rs .next ();
@@ -114,7 +116,7 @@ public void representNumberPropertyAsLong() throws Exception {
114116
115117 @ Test
116118 public void representNumberWithPrecisionPropertyAsDouble () throws Exception {
117- try (Connection c = SqlOnJson .convertPlain ("{a:[{o:0.009},{o:-12.45}]}" )) {
119+ try (Connection c = sqlOnJson .convertPlain ("{a:[{o:0.009},{o:-12.45}]}" )) {
118120 ResultSet rs = c .prepareStatement ("select * from a" ).executeQuery ();
119121 Assert .assertEquals ("DOUBLE" , rs .getMetaData ().getColumnTypeName (1 ));
120122 rs .next ();
@@ -126,7 +128,7 @@ public void representNumberWithPrecisionPropertyAsDouble() throws Exception {
126128
127129 @ Test
128130 public void representObjectWithArrayPropertyWithMissedAttributesAsTableInDbWithNull () throws Exception {
129- try (Connection c = SqlOnJson .convertPlain ("{a:[{id:12000,name:\" super\" },{id:90}]}" )) {
131+ try (Connection c = sqlOnJson .convertPlain ("{a:[{id:12000,name:\" super\" },{id:90}]}" )) {
130132 ResultSet rs = c .prepareStatement ("select * from a" ).executeQuery ();
131133 rs .next ();
132134 Assert .assertEquals (12000 , rs .getLong ("id" ));
@@ -139,7 +141,7 @@ public void representObjectWithArrayPropertyWithMissedAttributesAsTableInDbWithN
139141
140142 @ Test
141143 public void representObjectWithArrayPropertiesAsMultipleTables () throws Exception {
142- try (Connection c = SqlOnJson .convertPlain ("{orders:[{id:12}],history:[{orderId:12}]}" )) {
144+ try (Connection c = sqlOnJson .convertPlain ("{orders:[{id:12}],history:[{orderId:12}]}" )) {
143145 ResultSet rs1 = c .prepareStatement ("select * from orders" ).executeQuery ();
144146 rs1 .next ();
145147 Assert .assertEquals (12 , rs1 .getLong ("id" ));
@@ -152,7 +154,7 @@ public void representObjectWithArrayPropertiesAsMultipleTables() throws Exceptio
152154
153155 @ Test
154156 public void representEmbeddedObjectAsString () throws Exception {
155- try (Connection c = SqlOnJson .convertPlain ("{orders:[{em:{a:12}}]}" )) {
157+ try (Connection c = sqlOnJson .convertPlain ("{orders:[{em:{a:12}}]}" )) {
156158 ResultSet rs1 = c .prepareStatement ("select * from orders" ).executeQuery ();
157159 rs1 .next ();
158160 Assert .assertEquals ("{\" a\" :12}" , rs1 .getString ("em" ));
@@ -162,7 +164,7 @@ public void representEmbeddedObjectAsString() throws Exception {
162164
163165 @ Test
164166 public void supportEmbeddedArrayToTable () throws Exception {
165- try (Connection c = SqlOnJson .convertPlain ("{orders:[{em:[{a:-7}]}]}" )) {
167+ try (Connection c = sqlOnJson .convertPlain ("{orders:[{em:[{a:-7}]}]}" )) {
166168 ResultSet rs1 = c .prepareStatement ("select * from orders" ).executeQuery ();
167169 rs1 .next ();
168170 Assert .assertEquals ("[{\" a\" :-7}]" , rs1 .getString ("em" ));
@@ -172,10 +174,31 @@ public void supportEmbeddedArrayToTable() throws Exception {
172174
173175 @ Test
174176 public void supportEmbeddedObjectAsTable () throws Exception {
175- try (Connection c = SqlOnJson .convertPlain ("{orders:{em:[{a:-7}]}}" )) {
177+ try (Connection c = sqlOnJson .convertPlain ("{orders:{em:[{a:-7}]}}" )) {
176178 ResultSet rs1 = c .getMetaData ().getTables (null , null , "orders" , null );
177179 Assert .assertFalse (rs1 .next ());
178180 }
179181 }
180182
183+ @ Test
184+ public void supportRenamingOfColumnsForDefaultDb () throws Exception {
185+ try (Connection c = sqlOnJson .convertPlain ("{orders:[{user_id:12,id:900}],users:[{id:12}]}" )) {
186+ ResultSet rs1 = c .prepareStatement ("select o.id as oid, u.id as uid from orders o left join users u on user_id = u.id" ).executeQuery ();
187+ rs1 .next ();
188+ Assert .assertEquals ("12" , rs1 .getString ("uid" ));
189+ Assert .assertEquals ("900" , rs1 .getString ("oid" ));
190+ }
191+ }
192+
193+ @ Test
194+ public void supportCustomDb () throws Exception {
195+ try (Connection c = new SqlOnJson ("org.hsqldb.jdbc.JDBCDriver" , "jdbc:hsqldb:mem:sql_on_json_test;shutdown=true" , "sa" , "" )
196+ .convertPlain ("{orders:[{user_id:13,id:900}],users:[{id:13}]}" )) {
197+ ResultSet rs1 = c .prepareStatement ("select o.id as oid, u.id as uid from orders o left join users u on user_id = u.id" ).executeQuery ();
198+ rs1 .next ();
199+ Assert .assertEquals ("13" , rs1 .getString ("uid" ));
200+ Assert .assertEquals ("900" , rs1 .getString ("oid" ));
201+ }
202+ }
203+
181204}
0 commit comments