35
35
import com .mongodb .client .MongoCollection ;
36
36
import com .mongodb .client .MongoCursor ;
37
37
import com .mongodb .client .MongoDatabase ;
38
+ import com .mongodb .hibernate .internal .type .MqlType ;
38
39
import java .math .BigDecimal ;
39
40
import java .sql .Array ;
40
41
import java .sql .Date ;
45
46
import java .sql .Timestamp ;
46
47
import java .sql .Types ;
47
48
import java .util .Calendar ;
49
+ import java .util .List ;
48
50
import java .util .function .Consumer ;
51
+ import org .bson .BsonArray ;
52
+ import org .bson .BsonBoolean ;
49
53
import org .bson .BsonDocument ;
54
+ import org .bson .BsonInt32 ;
55
+ import org .bson .BsonObjectId ;
56
+ import org .bson .BsonString ;
50
57
import org .bson .Document ;
58
+ import org .bson .types .ObjectId ;
51
59
import org .junit .jupiter .api .BeforeEach ;
52
60
import org .junit .jupiter .api .DisplayName ;
53
61
import org .junit .jupiter .api .Nested ;
@@ -79,16 +87,18 @@ private MongoPreparedStatement createMongoPreparedStatement(String mql) throws S
79
87
private static final String EXAMPLE_MQL =
80
88
"""
81
89
{
82
- insert: "books ",
90
+ insert: "items ",
83
91
documents: [
84
92
{
85
- title: { $undefined: true },
86
- author: { $undefined: true },
87
- publishYear: { $undefined: true },
88
- outOfStock: { $undefined: true },
89
- tags: [
93
+ string1: { $undefined: true },
94
+ string2: { $undefined: true },
95
+ int32: { $undefined: true },
96
+ boolean: { $undefined: true },
97
+ stringAndObjectId: [
98
+ { $undefined: true },
90
99
{ $undefined: true }
91
100
]
101
+ objectId: { $undefined: true }
92
102
}
93
103
]
94
104
}
@@ -110,33 +120,33 @@ void testSuccess() throws SQLException {
110
120
111
121
try (var preparedStatement = createMongoPreparedStatement (EXAMPLE_MQL )) {
112
122
113
- preparedStatement .setString (1 , "War and Peace" );
114
- preparedStatement .setString (2 , "Leo Tolstoy" );
115
- preparedStatement .setInt (3 , 1869 );
116
- preparedStatement .setBoolean (4 , false );
117
- preparedStatement .setString (5 , "classic" );
123
+ preparedStatement .setString (1 , "s1" );
124
+ preparedStatement .setString (2 , "s2" );
125
+ preparedStatement .setInt (3 , 1 );
126
+ preparedStatement .setBoolean (4 , true );
127
+ preparedStatement .setString (5 , "array element" );
128
+ preparedStatement .setObject (6 , new ObjectId (1 , 2 ), MqlType .OBJECT_ID .getVendorTypeNumber ());
129
+ preparedStatement .setObject (7 , new ObjectId (2 , 0 ), MqlType .OBJECT_ID .getVendorTypeNumber ());
118
130
119
131
preparedStatement .executeUpdate ();
120
132
121
133
verify (mongoDatabase ).runCommand (eq (clientSession ), commandCaptor .capture ());
122
134
var command = commandCaptor .getValue ();
123
- var expectedDoc = BsonDocument .parse (
124
- """
125
- {
126
- insert: "books",
127
- documents: [
128
- {
129
- title: "War and Peace",
130
- author: "Leo Tolstoy",
131
- publishYear: 1869,
132
- outOfStock: false,
133
- tags: [
134
- "classic"
135
- ]
136
- }
137
- ]
138
- }
139
- """ );
135
+ var expectedDoc = new BsonDocument ()
136
+ .append ("insert" , new BsonString ("items" ))
137
+ .append (
138
+ "documents" ,
139
+ new BsonArray (List .of (new BsonDocument ()
140
+ .append ("string1" , new BsonString ("s1" ))
141
+ .append ("string2" , new BsonString ("s2" ))
142
+ .append ("int32" , new BsonInt32 (1 ))
143
+ .append ("boolean" , BsonBoolean .TRUE )
144
+ .append (
145
+ "stringAndObjectId" ,
146
+ new BsonArray (List .of (
147
+ new BsonString ("array element" ),
148
+ new BsonObjectId (new ObjectId (1 , 2 )))))
149
+ .append ("objectId" , new BsonObjectId (new ObjectId (2 , 0 ))))));
140
150
assertEquals (expectedDoc , command );
141
151
}
142
152
}
@@ -151,7 +161,7 @@ void testParameterIndexUnderflow() throws SQLSyntaxErrorException {
151
161
@ Test
152
162
void testParameterIndexOverflow () throws SQLSyntaxErrorException {
153
163
var mongoPreparedStatement = createMongoPreparedStatement (EXAMPLE_MQL );
154
- checkSetterMethods (mongoPreparedStatement , 6 , MongoPreparedStatementTests ::assertThrowsOutOfRangeException );
164
+ checkSetterMethods (mongoPreparedStatement , 8 , MongoPreparedStatementTests ::assertThrowsOutOfRangeException );
155
165
}
156
166
157
167
@ Nested
0 commit comments