1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) Massimo Cora' 2007 <maxcvs@email.it>
6 * anjuta is free software.
8 * You may redistribute it and/or modify it under the terms of the
9 * GNU General Public License, as published by the Free Software
10 * Foundation; either version 2 of the License, or (at your option)
13 * anjuta is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with anjuta. If not, write to:
20 * The Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301, USA.
25 #include <libanjuta/interfaces/ianjuta-iterable.h>
26 #include <libanjuta/anjuta-utils.h>
27 #include <libanjuta/anjuta-debug.h>
28 #include "symbol-db-engine-iterator-node.h"
29 #include "symbol-db-engine-iterator.h"
30 #include "symbol-db-engine.h"
32 struct _SymbolDBEngineIteratorPriv
34 GdaDataModel
*data_model
;
35 GdaDataModelIter
*data_iter
;
40 static SymbolDBEngineIteratorNodeClass
* parent_class
= NULL
;
43 sdb_engine_iterator_instance_init (SymbolDBEngineIterator
*object
)
45 SymbolDBEngineIterator
*sdbi
;
47 sdbi
= SYMBOL_DB_ENGINE_ITERATOR (object
);
48 sdbi
->priv
= g_new0 (SymbolDBEngineIteratorPriv
, 1);
50 /* initialize some priv data */
51 sdbi
->priv
->data_model
= NULL
;
52 sdbi
->priv
->data_iter
= NULL
;
53 sdbi
->priv
->total_rows
= -1;
57 sdb_engine_iterator_finalize (GObject
*object
)
59 SymbolDBEngineIterator
*dbi
;
60 SymbolDBEngineIteratorPriv
*priv
;
62 dbi
= SYMBOL_DB_ENGINE_ITERATOR (object
);
66 g_object_unref (priv
->data_model
);
69 G_OBJECT_CLASS (parent_class
)->finalize (object
);
73 sdb_engine_iterator_class_init (SymbolDBEngineIteratorClass
*klass
)
75 GObjectClass
* object_class
= G_OBJECT_CLASS (klass
);
76 parent_class
= g_type_class_ref (SYMBOL_TYPE_DB_ENGINE_ITERATOR_NODE
);
78 object_class
->finalize
= sdb_engine_iterator_finalize
;
81 SymbolDBEngineIterator
*
82 symbol_db_engine_iterator_new (GdaDataModel
*model
)
84 SymbolDBEngineIterator
*dbi
;
85 SymbolDBEngineIteratorPriv
*priv
;
87 g_return_val_if_fail (model
!= NULL
, NULL
);
89 dbi
= g_object_new (SYMBOL_TYPE_DB_ENGINE_ITERATOR
, NULL
);
93 priv
->data_model
= model
;
94 priv
->data_iter
= gda_data_model_iter_new (model
);
96 /* because gda_data_model_get_n_rows () could be cpu-intensive, we'll
97 * proxy this value, e.g. it's calculated if it is really needed */
98 priv
->total_rows
= -1;
100 /* to avoid calling everytime this function when we want to use the IteratorNode,
103 symbol_db_engine_iterator_first (dbi
);
105 /* set the data_iter no the base class */
106 symbol_db_engine_iterator_node_set_data (SYMBOL_DB_ENGINE_ITERATOR_NODE (dbi
),
109 ianjuta_iterable_first (IANJUTA_ITERABLE (dbi
), NULL
);
114 * Set the iterator at the first node.
117 symbol_db_engine_iterator_first (SymbolDBEngineIterator
*dbi
)
119 SymbolDBEngineIteratorPriv
*priv
;
121 g_return_val_if_fail (dbi
!= NULL
, FALSE
);
124 return gda_data_model_iter_set_at_row (priv
->data_iter
, 0);
129 symbol_db_engine_iterator_move_next (SymbolDBEngineIterator
*dbi
)
131 SymbolDBEngineIteratorPriv
*priv
;
133 g_return_val_if_fail (dbi
!= NULL
, FALSE
);
136 return gda_data_model_iter_move_next (priv
->data_iter
);
140 symbol_db_engine_iterator_move_prev (SymbolDBEngineIterator
*dbi
)
142 SymbolDBEngineIteratorPriv
*priv
;
144 g_return_val_if_fail (dbi
!= NULL
, FALSE
);
147 return gda_data_model_iter_move_prev (priv
->data_iter
);
151 * @return -1 on error
154 symbol_db_engine_iterator_get_n_items (SymbolDBEngineIterator
*dbi
)
156 SymbolDBEngineIteratorPriv
*priv
;
158 g_return_val_if_fail (dbi
!= NULL
, -1);
161 if (priv
->total_rows
< 0)
163 /* try to get the number of rows */
164 priv
->total_rows
= gda_data_model_get_n_rows (priv
->data_model
);
167 return priv
->total_rows
;
171 * Set the iterator at the last node.
174 symbol_db_engine_iterator_last (SymbolDBEngineIterator
*dbi
)
176 SymbolDBEngineIteratorPriv
*priv
;
178 g_return_val_if_fail (dbi
!= NULL
, FALSE
);
181 return gda_data_model_iter_set_at_row (priv
->data_iter
,
182 symbol_db_engine_iterator_get_n_items (dbi
));
186 symbol_db_engine_iterator_set_position (SymbolDBEngineIterator
*dbi
, gint pos
)
188 SymbolDBEngineIteratorPriv
*priv
;
190 g_return_val_if_fail (dbi
!= NULL
, FALSE
);
193 return gda_data_model_iter_set_at_row (priv
->data_iter
, pos
);
197 symbol_db_engine_iterator_get_position (SymbolDBEngineIterator
*dbi
)
199 SymbolDBEngineIteratorPriv
*priv
;
201 g_return_val_if_fail (dbi
!= NULL
, FALSE
);
204 return gda_data_model_iter_get_row (priv
->data_iter
);
209 symbol_db_engine_iterator_foreach (SymbolDBEngineIterator
*dbi
, GFunc callback
,
212 SymbolDBEngineIteratorPriv
*priv
;
214 g_return_if_fail (dbi
!= NULL
);
217 gint saved_pos
= symbol_db_engine_iterator_get_position (dbi
);
219 symbol_db_engine_iterator_last (dbi
);
221 while (!symbol_db_engine_iterator_move_next (dbi
))
222 callback (dbi
, user_data
);
223 symbol_db_engine_iterator_set_position (dbi
, saved_pos
);
226 /* IAnjutaIterable implementation */
229 isymbol_iter_first (IAnjutaIterable
*iterable
, GError
**err
)
231 SymbolDBEngineIterator
*dbi
= SYMBOL_DB_ENGINE_ITERATOR (iterable
);
232 SymbolDBEngineIteratorPriv
*priv
;
234 g_return_val_if_fail (dbi
!= NULL
, FALSE
);
238 if (symbol_db_engine_iterator_first (dbi
) == FALSE
)
240 symbol_db_engine_iterator_node_set_data (SYMBOL_DB_ENGINE_ITERATOR_NODE (iterable
),
245 symbol_db_engine_iterator_node_set_data (SYMBOL_DB_ENGINE_ITERATOR_NODE (iterable
),
251 isymbol_iter_next (IAnjutaIterable
*iterable
, GError
**err
)
253 SymbolDBEngineIterator
*dbi
= SYMBOL_DB_ENGINE_ITERATOR (iterable
);
254 SymbolDBEngineIteratorPriv
*priv
;
256 g_return_val_if_fail (dbi
!= NULL
, FALSE
);
259 if (symbol_db_engine_iterator_move_next (dbi
) == FALSE
)
261 symbol_db_engine_iterator_node_set_data (SYMBOL_DB_ENGINE_ITERATOR_NODE (iterable
),
266 symbol_db_engine_iterator_node_set_data (SYMBOL_DB_ENGINE_ITERATOR_NODE (iterable
),
273 isymbol_iter_previous (IAnjutaIterable
*iterable
, GError
**err
)
275 SymbolDBEngineIterator
*dbi
= SYMBOL_DB_ENGINE_ITERATOR (iterable
);
276 SymbolDBEngineIteratorPriv
*priv
;
278 g_return_val_if_fail (dbi
!= NULL
, FALSE
);
281 if (symbol_db_engine_iterator_move_prev (dbi
) == FALSE
)
283 symbol_db_engine_iterator_node_set_data (SYMBOL_DB_ENGINE_ITERATOR_NODE (iterable
),
288 symbol_db_engine_iterator_node_set_data (SYMBOL_DB_ENGINE_ITERATOR_NODE (iterable
),
295 isymbol_iter_last (IAnjutaIterable
*iterable
, GError
**err
)
297 SymbolDBEngineIterator
*dbi
= SYMBOL_DB_ENGINE_ITERATOR (iterable
);
298 SymbolDBEngineIteratorPriv
*priv
;
300 g_return_val_if_fail (dbi
!= NULL
, FALSE
);
303 if (symbol_db_engine_iterator_last (dbi
) == FALSE
)
305 symbol_db_engine_iterator_node_set_data (SYMBOL_DB_ENGINE_ITERATOR_NODE (iterable
),
310 symbol_db_engine_iterator_node_set_data (SYMBOL_DB_ENGINE_ITERATOR_NODE (iterable
),
317 isymbol_iter_foreach (IAnjutaIterable
*iterable
, GFunc callback
,
318 gpointer user_data
, GError
**err
)
320 SymbolDBEngineIterator
*dbi
= SYMBOL_DB_ENGINE_ITERATOR (iterable
);
321 SymbolDBEngineIteratorPriv
*priv
;
323 g_return_if_fail (dbi
!= NULL
);
326 symbol_db_engine_iterator_foreach (dbi
, callback
, user_data
);
330 isymbol_iter_set_position (IAnjutaIterable
*iterable
,
331 gint position
, GError
**err
)
333 SymbolDBEngineIterator
*dbi
= SYMBOL_DB_ENGINE_ITERATOR (iterable
);
334 SymbolDBEngineIteratorPriv
*priv
;
336 g_return_val_if_fail (dbi
!= NULL
, FALSE
);
339 if (symbol_db_engine_iterator_set_position (dbi
, position
) == FALSE
)
341 symbol_db_engine_iterator_node_set_data (SYMBOL_DB_ENGINE_ITERATOR_NODE (iterable
),
346 symbol_db_engine_iterator_node_set_data (SYMBOL_DB_ENGINE_ITERATOR_NODE (iterable
),
353 isymbol_iter_get_position (IAnjutaIterable
*iterable
, GError
**err
)
355 SymbolDBEngineIterator
*dbi
= SYMBOL_DB_ENGINE_ITERATOR (iterable
);
356 SymbolDBEngineIteratorPriv
*priv
;
358 g_return_val_if_fail (dbi
!= NULL
, FALSE
);
361 return symbol_db_engine_iterator_get_position (dbi
);
365 isymbol_iter_get_length (IAnjutaIterable
*iterable
, GError
**err
)
367 SymbolDBEngineIterator
*dbi
= SYMBOL_DB_ENGINE_ITERATOR (iterable
);
368 SymbolDBEngineIteratorPriv
*priv
;
370 g_return_val_if_fail (dbi
!= NULL
, FALSE
);
373 return symbol_db_engine_iterator_get_n_items (dbi
);
376 static IAnjutaIterable
*
377 isymbol_iter_clone (IAnjutaIterable
*iter
, GError
**e
)
379 DEBUG_PRINT ("isymbol_iter_clone () UNIMPLEMENTED");
384 isymbol_iter_assign (IAnjutaIterable
*iter
, IAnjutaIterable
*src_iter
, GError
**e
)
386 DEBUG_PRINT ("isymbol_iter_assign () UNIMPLEMENTED");
391 isymbol_iter_iface_init (IAnjutaIterableIface
*iface
, GError
**err
)
393 iface
->first
= isymbol_iter_first
;
394 iface
->next
= isymbol_iter_next
;
395 iface
->previous
= isymbol_iter_previous
;
396 iface
->last
= isymbol_iter_last
;
397 iface
->foreach
= isymbol_iter_foreach
;
398 iface
->set_position
= isymbol_iter_set_position
;
399 iface
->get_position
= isymbol_iter_get_position
;
400 iface
->get_length
= isymbol_iter_get_length
;
401 iface
->clone
= isymbol_iter_clone
;
402 iface
->assign
= isymbol_iter_assign
;
407 ANJUTA_TYPE_BEGIN (SymbolDBEngineIterator
, sdb_engine_iterator
, SYMBOL_TYPE_DB_ENGINE_ITERATOR_NODE
);
408 ANJUTA_TYPE_ADD_INTERFACE (isymbol_iter
, IANJUTA_TYPE_ITERABLE
);