File: DB.java

package info (click to toggle)
pilot-link 0.8.7-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 2,256 kB
  • ctags: 3,233
  • sloc: ansic: 24,039; java: 2,162; cpp: 1,641; sh: 1,585; makefile: 1,363; perl: 723; yacc: 660; python: 239; tcl: 14
file content (220 lines) | stat: -rw-r--r-- 5,931 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220

package Pdapilot;

import java.io.*;

/** A representation of an open database.
 */

public class DB {
		Dlp socket;
		int handle;
		String dbname;
		int dbmode;
		int dbcard;
		
		public Database dbClass;
		
		DB(Dlp socket, int handle, String dbname, int dbmode, int dbcard) {
			this.socket = socket;
			this.handle = handle;
			this.dbname = dbname;
			this.dbmode = dbmode;
			this.dbcard = dbcard;
			
			System.out.println("Opening database '"+dbname+"'");
			dbClass = (Database)Database.dbClasses.get(dbname);
			if (dbClass == null) {
				dbClass = Database.defaultDbClass;
				System.out.println("Using default dbclass");
			}
		}

		DB(Dlp socket, int handle, String dbname, int dbmode, int dbcard, Database dbClass) {
			this.socket = socket;
			this.handle = handle;
			this.dbname = dbname;
			this.dbmode = dbmode;
			this.dbcard = dbcard;
			this.dbClass = dbClass;
			
			System.out.println("Opening database '"+dbname+"'");
		}
		
		public void close() throws DlpException { 
			/* this method should be idempotent */
		    if (handle != 0)
				socket.dlp_CloseDB(handle);
			handle = 0;
		}
		
		protected void finalize() throws DlpException {
			this.close();
		}
		
		public AppBlock getAppBlock() throws DlpException {
			return socket.dlp_ReadAppBlock(handle, dbClass);
		}
		
		public AppBlock newAppBlock() {
			return dbClass.newAppBlock();
		}

		public SortBlock getSortBlock() throws DlpException {
			return socket.dlp_ReadSortBlock(handle, dbClass);
		}

		public SortBlock newSortBlock() {
			return dbClass.newSortBlock();
		}

		public Record getRecord(int index) throws DlpException {
			return socket.dlp_ReadRecordByIndex(handle, index, dbClass);
		}

		public Record newRecord() {
			return dbClass.newRecord();
		}

		public Record newRecord(RecordID id) {
			return dbClass.newRecord(id);
		}

		public Resource newResource() {
			return dbClass.newResource();
		}

		public Resource newResource(Char4 type, int id) {
			return dbClass.newResource(type, id);
		}
		
		public Record getRecord(RecordID id) throws DlpException {
			return socket.dlp_ReadRecordByID(handle, id, dbClass);
		}

		public Record getNextRecord(int category) throws DlpException {
			return socket.dlp_ReadNextRecInCategory(handle, category, dbClass);
		}

		public Record getNextModRecord() throws DlpException {
			return socket.dlp_ReadNextModifiedRec(handle, dbClass);
		}

		public Record getNextModRecord(int category) throws DlpException {
			return socket.dlp_ReadNextModifiedRecInCategory(handle, category, dbClass);
		}
		
		public Resource getResource(int index) throws DlpException {
			return socket.dlp_ReadResourceByIndex(handle, index, dbClass);
		}

		public Resource getResource(Char4 type, int id) throws DlpException {
			return socket.dlp_ReadResourceByType(handle, type, id, dbClass);
		}
		
		public long setRecord(Record record) throws DlpException {
			return socket.dlp_WriteRecord(handle, record);
		}

		public void setResource(Resource resource) throws DlpException {
			socket.dlp_WriteResource(handle, resource);
		}
		
		public void setAppBlock(AppBlock appblock) throws DlpException {
			socket.dlp_WriteAppBlock(handle, appblock);
		}

		public void setSortBlock(SortBlock sortblock) throws DlpException {
			socket.dlp_WriteSortBlock(handle, sortblock);
		}

		public void deleteRecord(RecordID id) throws DlpException {
			socket.dlp_DeleteRecord(handle, false, id);
		}

		public void deleteRecords() throws DlpException {
			socket.dlp_DeleteRecord(handle, true, null);
		}

		public void deleteResource(Char4 type, int id) throws DlpException {
			socket.dlp_DeleteResource(handle, false, type, id);
		}

		public void deleteResources() throws DlpException {
			socket.dlp_DeleteResource(handle, true, null, 0);
		}
		
		public void deleteCategory(int category) throws DlpException {
			socket.dlp_DeleteCategory(handle, category);
		}
		
		public void moveCategory(int from, int to) throws DlpException {
			socket.dlp_MoveCategory(handle, from, to);
		}

		public void purge() throws DlpException {
			socket.dlp_CleanUpDatabase(handle);
		}

		public void resetFlags() throws DlpException {
			socket.dlp_ResetSyncFlags(handle);
		}

		public void resetNext() throws DlpException {
			socket.dlp_ResetDBIndex(handle);
		}
		
		public int getRecords() throws DlpException {
			return socket.dlp_ReadOpenDBInfo(handle);
		}

		public Pref getPref(int id)
			throws DlpException, IOException
		{	return getPref(id, true); }
		
		public Pref getPref(int id, boolean backup)
			throws DlpException, NoCreatorException, IOException
		{
			if (socket.version() < 0x101)
				socket.dlp_CloseDB(handle);
			if (dbClass.creator() == null)
				throw new NoCreatorException();
			Pref result = socket.dlp_ReadAppPreference(dbClass.creator(), id, backup, dbClass);
			if (socket.version() < 0x101)
				handle = socket.dlp_OpenDB(dbcard, dbmode, dbname);
			return result;
		}
		
		public Pref newPref(int id)
		{
			return dbClass.newPref(null, dbClass.creator(), id, 1, true);
		}
		
		public Pref newPref(int id, int version, boolean backup)
		{
			return dbClass.newPref(null, dbClass.creator(), id, version, backup);
		}

		public void setPref(Pref pref)
			throws DlpException, IOException
		{
			if (socket.version() < 0x101)
				socket.dlp_CloseDB(handle);
			socket.dlp_WriteAppPreference(pref);
			if (socket.version() < 0x101)
				handle = socket.dlp_OpenDB(dbcard, dbmode, dbname);
		}

		public RecordID[] getRecordIDs()throws DlpException
			{ return getRecordIDs(false);	}
		public RecordID[] getRecordIDs(boolean sort) 	throws DlpException
		{	return getRecordIDs(sort, 0); 	}
		public RecordID[] getRecordIDs(boolean sort, int start)		throws DlpException
		{	return getRecordIDs(sort, start, 0xffff);	}
		public RecordID[] getRecordIDs(boolean sort, int start, int max)
			throws DlpException
		{
			return socket.dlp_ReadRecordIDList(handle, sort, start, max);
		}

}