1 /*
2 * Copyright 2015 MongoDB, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18 #include "mongoc-write-concern.h"
19 #include "mongoc-write-concern-private.h"
20 #include "mongoc-find-and-modify.h"
21 #include "mongoc-find-and-modify-private.h"
22 #include "mongoc-util-private.h"
23
24
25 /**
26 * mongoc_find_and_modify_new:
27 *
28 * Create a new mongoc_find_and_modify_t.
29 *
30 * Returns: A newly allocated mongoc_find_and_modify_t. This should be freed
31 * with mongoc_find_and_modify_destroy().
32 */
33 mongoc_find_and_modify_opts_t *
mongoc_find_and_modify_opts_new(void)34 mongoc_find_and_modify_opts_new (void)
35 {
36 mongoc_find_and_modify_opts_t *opts = NULL;
37
38 opts = (mongoc_find_and_modify_opts_t *) bson_malloc0 (sizeof *opts);
39 bson_init (&opts->extra);
40 opts->bypass_document_validation = MONGOC_BYPASS_DOCUMENT_VALIDATION_DEFAULT;
41
42 return opts;
43 }
44
45 bool
mongoc_find_and_modify_opts_set_sort(mongoc_find_and_modify_opts_t * opts,const bson_t * sort)46 mongoc_find_and_modify_opts_set_sort (mongoc_find_and_modify_opts_t *opts,
47 const bson_t *sort)
48 {
49 BSON_ASSERT (opts);
50
51 if (sort) {
52 _mongoc_bson_destroy_if_set (opts->sort);
53 opts->sort = bson_copy (sort);
54 return true;
55 }
56
57 return false;
58 }
59
60 void
mongoc_find_and_modify_opts_get_sort(const mongoc_find_and_modify_opts_t * opts,bson_t * sort)61 mongoc_find_and_modify_opts_get_sort (const mongoc_find_and_modify_opts_t *opts,
62 bson_t *sort)
63 {
64 BSON_ASSERT (opts);
65 BSON_ASSERT (sort);
66
67 if (opts->sort) {
68 bson_copy_to (opts->sort, sort);
69 } else {
70 bson_init (sort);
71 }
72 }
73
74 bool
mongoc_find_and_modify_opts_set_update(mongoc_find_and_modify_opts_t * opts,const bson_t * update)75 mongoc_find_and_modify_opts_set_update (mongoc_find_and_modify_opts_t *opts,
76 const bson_t *update)
77 {
78 BSON_ASSERT (opts);
79
80 if (update) {
81 _mongoc_bson_destroy_if_set (opts->update);
82 opts->update = bson_copy (update);
83 return true;
84 }
85
86 return false;
87 }
88
89 void
mongoc_find_and_modify_opts_get_update(const mongoc_find_and_modify_opts_t * opts,bson_t * update)90 mongoc_find_and_modify_opts_get_update (
91 const mongoc_find_and_modify_opts_t *opts, bson_t *update)
92 {
93 BSON_ASSERT (opts);
94 BSON_ASSERT (update);
95
96 if (opts->update) {
97 bson_copy_to (opts->update, update);
98 } else {
99 bson_init (update);
100 }
101 }
102
103 bool
mongoc_find_and_modify_opts_set_fields(mongoc_find_and_modify_opts_t * opts,const bson_t * fields)104 mongoc_find_and_modify_opts_set_fields (mongoc_find_and_modify_opts_t *opts,
105 const bson_t *fields)
106 {
107 BSON_ASSERT (opts);
108
109 if (fields) {
110 _mongoc_bson_destroy_if_set (opts->fields);
111 opts->fields = bson_copy (fields);
112 return true;
113 }
114
115 return false;
116 }
117
118 void
mongoc_find_and_modify_opts_get_fields(const mongoc_find_and_modify_opts_t * opts,bson_t * fields)119 mongoc_find_and_modify_opts_get_fields (
120 const mongoc_find_and_modify_opts_t *opts, bson_t *fields)
121 {
122 BSON_ASSERT (opts);
123 BSON_ASSERT (fields);
124
125 if (opts->fields) {
126 bson_copy_to (opts->fields, fields);
127 } else {
128 bson_init (fields);
129 }
130 }
131
132 bool
mongoc_find_and_modify_opts_set_flags(mongoc_find_and_modify_opts_t * opts,const mongoc_find_and_modify_flags_t flags)133 mongoc_find_and_modify_opts_set_flags (
134 mongoc_find_and_modify_opts_t *opts,
135 const mongoc_find_and_modify_flags_t flags)
136 {
137 BSON_ASSERT (opts);
138
139 opts->flags = flags;
140 return true;
141 }
142
143 mongoc_find_and_modify_flags_t
mongoc_find_and_modify_opts_get_flags(const mongoc_find_and_modify_opts_t * opts)144 mongoc_find_and_modify_opts_get_flags (
145 const mongoc_find_and_modify_opts_t *opts)
146 {
147 BSON_ASSERT (opts);
148
149 return opts->flags;
150 }
151
152 bool
mongoc_find_and_modify_opts_set_bypass_document_validation(mongoc_find_and_modify_opts_t * opts,bool bypass)153 mongoc_find_and_modify_opts_set_bypass_document_validation (
154 mongoc_find_and_modify_opts_t *opts, bool bypass)
155 {
156 BSON_ASSERT (opts);
157
158 opts->bypass_document_validation =
159 bypass ? MONGOC_BYPASS_DOCUMENT_VALIDATION_TRUE
160 : MONGOC_BYPASS_DOCUMENT_VALIDATION_FALSE;
161 return true;
162 }
163
164 bool
mongoc_find_and_modify_opts_get_bypass_document_validation(const mongoc_find_and_modify_opts_t * opts)165 mongoc_find_and_modify_opts_get_bypass_document_validation (
166 const mongoc_find_and_modify_opts_t *opts)
167 {
168 BSON_ASSERT (opts);
169
170 return opts->bypass_document_validation ==
171 MONGOC_BYPASS_DOCUMENT_VALIDATION_TRUE;
172 }
173
174 bool
mongoc_find_and_modify_opts_set_max_time_ms(mongoc_find_and_modify_opts_t * opts,uint32_t max_time_ms)175 mongoc_find_and_modify_opts_set_max_time_ms (
176 mongoc_find_and_modify_opts_t *opts, uint32_t max_time_ms)
177 {
178 BSON_ASSERT (opts);
179
180 opts->max_time_ms = max_time_ms;
181 return true;
182 }
183
184 uint32_t
mongoc_find_and_modify_opts_get_max_time_ms(const mongoc_find_and_modify_opts_t * opts)185 mongoc_find_and_modify_opts_get_max_time_ms (
186 const mongoc_find_and_modify_opts_t *opts)
187 {
188 BSON_ASSERT (opts);
189
190 return opts->max_time_ms;
191 }
192
193 bool
mongoc_find_and_modify_opts_append(mongoc_find_and_modify_opts_t * opts,const bson_t * extra)194 mongoc_find_and_modify_opts_append (mongoc_find_and_modify_opts_t *opts,
195 const bson_t *extra)
196 {
197 BSON_ASSERT (opts);
198 BSON_ASSERT (extra);
199
200 return bson_concat (&opts->extra, extra);
201 }
202
203 void
mongoc_find_and_modify_opts_get_extra(const mongoc_find_and_modify_opts_t * opts,bson_t * extra)204 mongoc_find_and_modify_opts_get_extra (
205 const mongoc_find_and_modify_opts_t *opts, bson_t *extra)
206 {
207 BSON_ASSERT (opts);
208 BSON_ASSERT (extra);
209
210 bson_copy_to (&opts->extra, extra);
211 }
212
213 /**
214 * mongoc_find_and_modify_opts_destroy:
215 * @opts: A mongoc_find_and_modify_opts_t.
216 *
217 * Releases a mongoc_find_and_modify_opts_t and all associated memory.
218 */
219 void
mongoc_find_and_modify_opts_destroy(mongoc_find_and_modify_opts_t * opts)220 mongoc_find_and_modify_opts_destroy (mongoc_find_and_modify_opts_t *opts)
221 {
222 if (opts) {
223 _mongoc_bson_destroy_if_set (opts->sort);
224 _mongoc_bson_destroy_if_set (opts->update);
225 _mongoc_bson_destroy_if_set (opts->fields);
226 bson_destroy (&opts->extra);
227 bson_free (opts);
228 }
229 }
230