View Javadoc
1   /*
2   Copyright (c) 2016 James Ahlborn
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  package com.healthmarketscience.jackcess.impl;
18  
19  import java.io.IOException;
20  import java.nio.charset.Charset;
21  
22  
23  /**
24   * Common helper class used to maintain state during database mutation.
25   *
26   * @author James Ahlborn
27   */
28  abstract class DBMutator 
29  {
30    private final DatabaseImpl _database;
31  
32    protected DBMutator(DatabaseImpl database) {
33      _database = database;
34    }
35  
36    public DatabaseImpl getDatabase() {
37      return _database;
38    }
39  
40    public JetFormat getFormat() {
41      return _database.getFormat();
42    }
43  
44    public PageChannel getPageChannel() {
45      return _database.getPageChannel();
46    }
47  
48    public Charset getCharset() {
49      return _database.getCharset();
50    }
51  
52    public int reservePageNumber() throws IOException {
53      return getPageChannel().allocateNewPage();
54    }
55  
56    public static int calculateNameLength(String name) {
57      return (name.length() * JetFormat.TEXT_FIELD_UNIT_SIZE) + 2;
58    }
59  
60    protected ColumnImpl.SortOrder getDbSortOrder() {
61      try {
62        return _database.getDefaultSortOrder();
63      } catch(IOException e) {
64        // ignored, just use the jet format default
65      }
66      return null;
67    }
68  }