File: RecordCollection.vala

package info (click to toggle)
libgda5 5.2.4-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 75,256 kB
  • ctags: 49,218
  • sloc: ansic: 461,759; sh: 11,808; xml: 10,466; yacc: 5,160; makefile: 4,327; php: 1,416; java: 1,300; python: 896; sql: 879; perl: 116
file content (140 lines) | stat: -rw-r--r-- 3,647 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* -*- Mode: Vala; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * libgdadata
 * Copyright (C) Daniel Espinosa Ortiz 2011 <esodan@gmail.com>
 * 
 * libgda is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * libgda is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
using Gee;
using Gda;

namespace GdaData {
	public class RecordCollection : AbstractCollection<DbRecord>, DbRecordCollection
	{
		private DataModel _model;
		private DbTable   _table;
		
		public DbTable    table { get { return table; } }
		
		public DataModel  model { get { return _model; } }
		
		public Connection connection { get; set; }
		
		public RecordCollection (DataModel m, DbTable table)
		{
			_model = m;
			_table = table;
		}
		// AbstractCollection Implementation
		public override bool add (DbRecord item) 
		{ 
			try {
				int r = _model.append_row ();
				foreach (DbField f in item.fields) {
					_model.set_value_at (_model.get_column_index (f.name), r, f.value);
				}
				return true;
			} 
			catch (Error e) { GLib.warning (e.message); }
			return false; 
		}
		public override void clear ()
		{ 
			try {
				var iter = _model.create_iter ();
				while (iter.move_next ()) {
					_model.remove_row (iter.get_row ());
				}
				((DataProxy) _model).apply_all_changes ();
			}
			catch (Error e) { GLib.warning (e.message); }
		}
		public override bool contains (DbRecord item)
		{
			bool found = true;
			var iter = _model.create_iter ();
			while (iter.move_next ()) {
				foreach (DbField k in item.keys) {
					try {
						Value id = iter.get_value_at (iter.data_model.get_column_index (k.name));
						Value v = k.value;
						if (Gda.value_compare (id,v) != 0)
							found = false;
					}
					catch (Error e) { 
						GLib.warning (e.message); 
						found = false;
					}
				}
				if (found) break;
			}
			return found;
		}
		public override Gee.Iterator<DbRecord> iterator () 
		{ 
			Gda.DataModelIter iter;
			iter = _model.create_iter ();
			return new RecordCollectionIterator (iter, _table); 
		}
		public override bool remove (DbRecord item)
		{
			var iter = _model.create_iter ();
			while (iter.move_next ()) {
				bool found = true;
				foreach (DbField k in item.keys) {
					try {
						Value id = iter.get_value_at (iter.data_model.get_column_index (k.name));
						Value v = k.value;
						if (Gda.value_compare (id,v) != 0)
							found = false;
					}
					catch (Error e) { 
						GLib.warning (e.message);
						found = false;
					}
				}
				if (found) {
					try {
						_model.remove_row (iter.get_row ());
						((DataProxy)_model).apply_all_changes ();
						return true;
					} catch {}
				}
			}
			return false;
		}
		public override bool read_only { 
			get {
				var f = _model.get_access_flags ();
				if ( (f & Gda.DataModelAccessFlags.INSERT
					& Gda.DataModelAccessFlags.UPDATE
					& Gda.DataModelAccessFlags.DELETE) != 0 )
					return true;
				else
					return false;
			}
		}
		public override int size { 
			get {
				return _model.get_n_rows ();
			} 
		}
		// 
		public string to_string ()
		{
			return _model.dump_as_string ();
		}
	}
}