/
baseExtension.ts
183 lines (170 loc) · 5.16 KB
/
baseExtension.ts
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
import { Extensions } from './extensions';
import { ExtensionsDao } from './extensionsDao';
import { ExtensionScopeType } from './extensionScopeType';
import type { GeoPackage } from '../geoPackage';
import type { GeoPackageConnection } from '../db/geoPackageConnection';
import { GeoPackageException } from '../geoPackageException';
/**
* Abstract base GeoPackage extension
*/
export abstract class BaseExtension {
/**
* GeoPackage
*/
readonly geoPackage: GeoPackage;
/**
* Connection to the GeoPackage
*/
protected readonly connection: GeoPackageConnection;
/**
* ExtensionDao
*/
readonly extensionsDao: ExtensionsDao;
/**
* Name of the extension
*/
extensionName: string;
/**
* Definition for the extension
*/
extensionDefinition: string;
/**
* extension table name
*/
tableName: string;
/**
*
* @param geoPackage GeoPackage object
*/
constructor(geoPackage: GeoPackage) {
this.geoPackage = geoPackage;
this.connection = geoPackage.getConnection();
this.extensionsDao = geoPackage.getExtensionsDao();
}
/**
* Get the geopackage
*/
getGeoPackage(): GeoPackage {
return this.geoPackage;
}
/**
* Get the extension or create as needed
* @param {String} extensionName extension name
* @param {String} tableName table name
* @param {String} columnName column name
* @param {String} definition extension definition
* @param {String} scopeType extension scope type
* @return {Extensions}
*/
getOrCreate(
extensionName: string,
tableName: string | null,
columnName: string | null,
definition: string,
scopeType: ExtensionScopeType,
): Extensions {
let extension = this.get(extensionName, tableName, columnName);
if (extension == null) {
try {
if (!this.extensionsDao.isTableExists()) {
this.geoPackage.createExtensionsTable();
}
extension = new Extensions();
extension.setTableName(tableName);
extension.setColumnName(columnName);
extension.setExtensionName(extensionName);
extension.setDefinition(definition);
extension.setScope(scopeType);
this.extensionsDao.create(extension);
} catch (e) {
throw new GeoPackageException(
"Failed to create '" +
extensionName +
"' extension for GeoPackage: " +
this.geoPackage.getName() +
', Table Name: ' +
tableName +
', Column Name: ' +
columnName,
);
}
}
return extension;
}
/**
* Get the extension for the name, table name and column name
* @param {String} extensionName extension name
* @param {String} tableName table name
* @param {String} columnName column name
* @return {Extensions[]}
*/
get(extensionName: string, tableName?: string, columnName?: string): Extensions {
let extension = null;
if (this.extensionsDao.isTableExists()) {
const extensions = this.extensionsDao.queryByExtensionAndTableNameAndColumnName(
extensionName,
tableName,
columnName,
);
if (extensions.length > 0) {
extension = extensions[0];
}
}
return extension;
}
/**
* Get the extension for the name, table name and column name
* @param {String} extensionName extension name
* @param {String} tableName table name
* @param {String} columnName column name
* @return {Extensions[]}
*/
getExtension(extensionName: string, tableName?: string | null, columnName?: string | null): Extensions[] {
if (!this.extensionsDao.isTableExists()) {
return [];
}
return this.extensionsDao.queryByExtensionAndTableNameAndColumnName(extensionName, tableName, columnName);
}
/**
* Determine if the GeoPackage has the extension
* @param {String} extensionName extension name
* @param {String} tableName table name
* @param {String} columnName column name
* @returns {Boolean} if the extension exists
*/
hasExtension(extensionName: string, tableName: string, columnName: string): boolean {
return !!this.getExtension(extensionName, tableName, columnName).length;
}
hasExtensions(extensionName: string): boolean {
return this.extensionsDao.isTableExists() && this.extensionsDao.queryAllByExtension(extensionName).length !== 0;
}
/**
* Verify the GeoPackage is writable and throw an exception if it is not
*/
public verifyWritable(): void {
this.geoPackage.verifyWritable();
}
/**
* Create the extension
* @param {string} extensionName
* @param {string} tableName
* @param {string} columnName
* @param {string} definition
* @param {string} scopeType
*/
createExtension(
extensionName: string,
tableName: string,
columnName: string,
definition: string,
scopeType: string,
): number {
const extension = new Extensions();
extension.setTableName(tableName);
extension.setColumnName(columnName);
extension.setExtensionName(extensionName);
extension.setDefinition(definition);
extension.setScope(scopeType);
return this.extensionsDao.create(extension);
}
}