File: UnaryTableSequence.java

package info (click to toggle)
eclipselink 2.7.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 44,708 kB
  • sloc: java: 476,807; xml: 547; makefile: 21
file content (183 lines) | stat: -rw-r--r-- 5,513 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
/*
 * Copyright (c) 1998, 2018 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0,
 * or the Eclipse Distribution License v. 1.0 which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
 */

// Contributors:
//     Oracle - initial API and implementation from Oracle TopLink
package org.eclipse.persistence.sequencing;

import java.io.StringWriter;
import org.eclipse.persistence.queries.*;

/**
 * <p>
 * <b>Purpose</b>: Defines sequencing through using a singleton sequence table.
 * <p>
 * <b>Description</b>
 * This is similar to the TableSequence but the sequence table only stores a single
 * row defining a single sequence.
 */
public class UnaryTableSequence extends QuerySequence {

    /** Hold the name of the only column in multiple single-column tables */
    protected String counterFieldName = "SEQUENCE";
    protected String updateString1;
    protected String updateString2;
    protected String selectString1;
    protected String selectString2;
    protected int updateStringBufferSize;
    protected int selectStringBufferSize;

    public UnaryTableSequence() {
        super(false, true);
    }

    /**
     * Create a new sequence with the name.
     */
    public UnaryTableSequence(String name) {
        super(name, false, true);
    }

    /**
     * Create a new sequence with the name and sequence pre-allocation size.
     */
    public UnaryTableSequence(String name, int size) {
        super(name, size, false, true);
    }

    public UnaryTableSequence(String name, String counterFieldName) {
        this(name);
        setCounterFieldName(counterFieldName);
    }

    public UnaryTableSequence(String name, int size, String counterFieldName) {
        this(name, size);
        setCounterFieldName(counterFieldName);
    }

    public boolean isUnaryTable() {
        return true;
    }

    public boolean equals(Object obj) {
        if (obj instanceof UnaryTableSequence) {
            UnaryTableSequence other = (UnaryTableSequence)obj;
            if (equalNameAndSize(this, other)) {
                return getCounterFieldName().equals(other.getCounterFieldName());
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        String counterFieldName = getCounterFieldName();
        result = 31 * result + (counterFieldName != null ? counterFieldName.hashCode() : 0);
        return result;
    }

    public void setCounterFieldName(String name) {
        this.counterFieldName = name;
    }

    public String getCounterFieldName() {
        return counterFieldName;
    }

    /**
    * INTERNAL:
    */
    public void onConnect() {
        super.onConnect();
        initialize();
    }

    /**
    * INTERNAL:
    */
    public void onDisconnect() {
        clear();
        super.onDisconnect();
    }

    protected ValueReadQuery buildSelectQuery(String seqName, Integer size) {
        ValueReadQuery selectQuery = new ValueReadQuery();
        selectQuery.dontBindAllParameters();
        StringWriter writer = new StringWriter(selectStringBufferSize + seqName.length());
        writer.write(selectString1);
        writer.write(seqName);
        selectQuery.setSQLString(writer.toString());
        return selectQuery;
    }

    protected DataModifyQuery buildUpdateQuery(String seqName, Number size) {
        DataModifyQuery updateQuery = new DataModifyQuery();
        updateQuery.dontBindAllParameters();
        String sizeString = size.toString();
        StringWriter writer = new StringWriter(updateStringBufferSize + seqName.length() + sizeString.length());
        writer.write(updateString1);
        writer.write(seqName);
        writer.write(updateString2);
        writer.write(sizeString);
        updateQuery.setSQLString(writer.toString());
        return updateQuery;
    }

    protected void initialize() {
        if (getSelectQuery() == null) {
            buildSelectString1();
            selectStringBufferSize = selectString1.length();
        }
        if ((getUpdateQuery() == null) && !shouldSkipUpdate()) {
            buildUpdateString1();
            buildUpdateString2();
            updateStringBufferSize = updateString1.length() + updateString2.length();
        }
    }

    protected void buildUpdateString1() {
        updateString1 = "UPDATE ";
        if (qualifier != "") {
            updateString1 = updateString1 + qualifier + '.';
        }
    }

    protected void buildUpdateString2() {
        StringWriter writer = new StringWriter();
        writer.write(" SET ");
        writer.write(getCounterFieldName());
        writer.write(" = ");
        writer.write(getCounterFieldName());
        writer.write(" + ");
        updateString2 = writer.toString();
    }

    protected void buildSelectString1() {
        selectString1 = "SELECT * FROM ";
        if (qualifier != "") {
            selectString1 = selectString1 + qualifier + '.';
        }
    }

    protected void clear() {
        updateString1 = null;
        updateString2 = null;
        selectString1 = null;
        selectString2 = null;
        updateStringBufferSize = 0;
        selectStringBufferSize = 0;
    }
}