1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.tentackle.dbms;
20
21 import org.tentackle.common.Service;
22 import org.tentackle.session.PersistenceException;
23 import org.tentackle.sql.Backend;
24
25 import java.util.StringTokenizer;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 @Service(IdSourceConfigurator.class)
42 public class DefaultIdSourceConfigurator implements IdSourceConfigurator {
43
44
45 public static final String TABLE_BASED = "table";
46
47
48 public static final String SEQUENCE_BASED = "sequence";
49
50
51
52
53
54 public DefaultIdSourceConfigurator() {
55
56 }
57
58 @Override
59 public IdSource configure(String idConfig) {
60
61 String type = null;
62 String name = null;
63 Integer sequenceMultiplier = null;
64 Integer globalMultiplier = null;
65
66 StringTokenizer stok = new StringTokenizer(idConfig, ":");
67 while (stok.hasMoreTokens()) {
68 String token = stok.nextToken();
69 if (type == null) {
70 type = token;
71 }
72 else if (name == null) {
73 name = token;
74 }
75 else if (sequenceMultiplier == null) {
76 sequenceMultiplier = Integer.parseInt(token);
77 }
78 else if (globalMultiplier == null) {
79 globalMultiplier = Integer.parseInt(token);
80 }
81 else {
82 throw new PersistenceException("malformed idSource configuration: " + idConfig);
83 }
84 }
85
86 if (type == null) {
87 throw new PersistenceException("empty id configuration");
88 }
89
90 return switch (type) {
91 case SEQUENCE_BASED -> {
92 if (sequenceMultiplier == null) {
93 yield ObjectSequenceId.get(name);
94 }
95 else if (globalMultiplier != null) {
96 yield ObjectSequenceId.get(name, sequenceMultiplier, globalMultiplier);
97 }
98 throw new PersistenceException("sequenceMultiplier and globalMultiplier must be defined both or not at all");
99 }
100 case TABLE_BASED -> ObjectId.get(name);
101 default -> throw new PersistenceException("unsupported idSource type: " + type);
102 };
103 }
104
105 @Override
106 public IdSource configure(Backend backend) {
107 if (backend.isSequenceSupported()) {
108 return configure(SEQUENCE_BASED + ":object_sequence_id");
109 }
110 else {
111 return configure(TABLE_BASED + ":object_id");
112 }
113 }
114
115 }