OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 142 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 142 Rev 182
Line 1... Line 1...
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
9
 * language governing permissions and limitations under the License.
Line 14... Line 14...
14
 package org.openconcerto.sql.model;
14
 package org.openconcerto.sql.model;
15
 
15
 
16
import org.openconcerto.sql.utils.SQL_URL;
16
import org.openconcerto.sql.utils.SQL_URL;
17
import org.openconcerto.utils.EnumOrderedSet;
17
import org.openconcerto.utils.EnumOrderedSet;
18
import org.openconcerto.utils.Tuple2;
18
import org.openconcerto.utils.Tuple2;
-
 
19
import org.openconcerto.utils.Tuple2.List2;
19
import org.openconcerto.utils.cc.ITransformer;
20
import org.openconcerto.utils.cc.ITransformer;
20
 
21
 
21
import java.sql.Statement;
22
import java.sql.Statement;
22
import java.util.EnumSet;
23
import java.util.EnumSet;
23
import java.util.HashMap;
24
import java.util.HashMap;
24
import java.util.Iterator;
25
import java.util.Iterator;
25
import java.util.Map;
26
import java.util.Map;
-
 
27
import java.util.Objects;
26
import java.util.Set;
28
import java.util.Set;
27
 
29
 
-
 
30
import org.h2.engine.ConnectionInfo;
28
import org.h2.engine.Constants;
31
import org.h2.engine.Constants;
29
import org.h2.util.StringUtils;
32
import org.h2.util.StringUtils;
30
 
33
 
31
/**
34
/**
32
 * A RDBMS like PostgreSQL or MySQL.
35
 * A RDBMS like PostgreSQL or MySQL.
Line 133... Line 136...
133
     * @see <a href="http://www.h2database.com/">H2 site</a>
136
     * @see <a href="http://www.h2database.com/">H2 site</a>
134
     */
137
     */
135
    H2("H2") {
138
    H2("H2") {
136
 
139
 
137
        private static final String TCP_PREFIX = "tcp://";
140
        private static final String TCP_PREFIX = "tcp://";
138
        private static final String SSL_PREFIX = "ssl://";
141
        private static final String ARBITRARY_BASE_NAME = "foo";
139
 
142
 
140
        ITransformer<String, String> getURLTransf(final SQLServer s) {
143
        ITransformer<String, String> getURLTransf(final SQLServer s) {
141
            if (s.getSQLSystem() != this)
144
            if (s.getSQLSystem() != this)
142
                throw new IllegalArgumentException(s + " is not " + this);
145
                throw new IllegalArgumentException(s + " is not " + this);
143
 
146
 
144
            return new ITransformer<String, String>() {
147
            return new ITransformer<String, String>() {
145
                @Override
148
                @Override
146
                public String transformChecked(String base) {
149
                public String transformChecked(String base) {
147
                    final String sep;
-
 
148
                    // allow one to use mem for server name
-
 
149
                    // otherwise just cat name and base
-
 
150
                    // (eg "tcp://127.0.0.1/" + "sample", "file:~/" + "sample" or "" + "sample" )
-
 
151
                    if (s.getName().equals("mem"))
-
 
152
                        sep = ":";
-
 
153
                    else {
-
 
154
                        // for file, pass either file:, or file:/someDir/
-
 
155
                        // jdbc:h2:~/test
-
 
156
                        sep = "";
-
 
157
                    }
-
 
158
                    // by default h2 convert database name to upper case (we used to work around it
150
                    // by default h2 convert database name to upper case (we used to work around it
159
                    // with SQLSystem.getMDName() but in r2251 an equalsIgnoreCase() was replaced by
151
                    // with SQLSystem.getMDName() but in r2251 an equalsIgnoreCase() was replaced by
160
                    // equals()) see http://code.google.com/p/h2database/issues/detail?id=204
152
                    // equals()) see http://code.google.com/p/h2database/issues/detail?id=204
161
                    return s.getName() + sep + base + ";DATABASE_TO_UPPER=false";
153
                    return s.getName() + base + ";DATABASE_TO_UPPER=false";
162
                }
154
                }
163
            };
155
            };
164
        }
156
        }
165
 
157
 
166
        @Override
158
        @Override
Line 185... Line 177...
185
        public String getServerName(final String host) {
177
        public String getServerName(final String host) {
186
            return TCP_PREFIX + host + "/";
178
            return TCP_PREFIX + host + "/";
187
        }
179
        }
188
 
180
 
189
        @Override
181
        @Override
190
        public String getHostname(final String server) {
182
        public List2<String> getHostnameAndPath(final String server) {
-
 
183
            // append base name to server name to get a valid value
-
 
184
            final ConnectionInfo info = new ConnectionInfo(server + ARBITRARY_BASE_NAME);
-
 
185
            final String name = info.getName();
191
            final String prefix;
186
            final String hostName;
-
 
187
            final int pathIndex;
192
            if (server.startsWith(TCP_PREFIX))
188
            if (info.isRemote()) {
193
                prefix = TCP_PREFIX;
189
                // tcp:// or ssl://server/path
194
            else if (server.startsWith(SSL_PREFIX))
190
                assert name.startsWith("//");
-
 
191
                final int slashIndex = name.indexOf('/', 2);
-
 
192
                hostName = name.substring(2, slashIndex);
195
                prefix = SSL_PREFIX;
193
                pathIndex = slashIndex + 1;
196
            else
194
            } else {
-
 
195
                // mem: or file:/data/sample or ~/test
197
                return null;
196
                hostName = null;
-
 
197
                pathIndex = 0;
-
 
198
            }
198
 
199
 
199
            // check that our name doesn't contain a path, otherwise we would loose it
200
            return new List2<>(hostName, name.substring(pathIndex, name.length() - ARBITRARY_BASE_NAME.length()));
200
            // eg dbserv:8084/~/sample
201
        }
201
            final String hostAndPath = server.substring(prefix.length());
-
 
-
 
202
 
202
            final int firstSlash = hostAndPath.indexOf('/');
203
        @Override
203
            if (firstSlash == hostAndPath.lastIndexOf('/')) {
204
        public boolean isPermanent(final String server) {
204
                return hostAndPath.substring(0, firstSlash);
205
            return !server.startsWith(H2_IN_MEMORY);
205
            } else
-
 
206
                return null;
-
 
207
        }
206
        }
208
 
207
 
209
        @Override
208
        @Override
210
        public Map<String, String> getConnectionInfo(final String url) {
209
        public Map<String, String> getConnectionInfo(final String url) {
211
            final Tuple2<String, Map<String, String>> settings = readSettingsFromURL(url);
210
            final Tuple2<String, Map<String, String>> settings = readSettingsFromURL(url);
Line 314... Line 313...
314
            else
313
            else
315
                throw e;
314
                throw e;
316
        }
315
        }
317
    }
316
    }
318
 
317
 
-
 
318
    public static final String H2_IN_MEMORY = "mem:";
-
 
319
 
319
    private final String label;
320
    private final String label;
320
    private final EnumOrderedSet<HierarchyLevel> levels;
321
    private final EnumOrderedSet<HierarchyLevel> levels;
321
 
322
 
322
    private SQLSystem(final String label) {
323
    private SQLSystem(final String label) {
323
        this.label = label;
324
        this.label = label;
Line 420... Line 421...
420
    }
421
    }
421
 
422
 
422
    /**
423
    /**
423
     * The host name for the passed server.
424
     * The host name for the passed server.
424
     * 
425
     * 
425
     * @param server the name of an {@link SQLServer}.
426
     * @param server the name of an {@link SQLServer}, e.g. tcp://127.0.0.1/dir/.
426
     * @return its host or <code>null</code> if <code>server</code> has no host or is too complex
427
     * @return its host and its path, both can be <code>null</code> (but not at the same time), e.g.
427
     *         (eg tcp://127.0.0.1/a/b/c).
428
     *         [127.0.0.1, dir].
-
 
429
     */
-
 
430
    public List2<String> getHostnameAndPath(String server) {
-
 
431
        Objects.requireNonNull(server, "Null server");
-
 
432
        return new List2<>(server, null);
-
 
433
    }
-
 
434
 
-
 
435
    /**
-
 
436
     * Whether the passed server runs inside the VM.
-
 
437
     * 
-
 
438
     * @param server a server, e.g. {@link SQLServer#getName()}.
-
 
439
     * @return <code>true</code> if <code>server</code> runs inside the VM.
-
 
440
     */
-
 
441
    public final boolean isEmbedded(final String server) {
-
 
442
        return getHostnameAndPath(server).get0() == null;
-
 
443
    }
-
 
444
 
-
 
445
    /**
-
 
446
     * Whether the passed server survives when all connections to it are closed.
-
 
447
     * 
-
 
448
     * @param server a server, e.g. {@link SQLServer#getName()}.
-
 
449
     * @return <code>true</code> if <code>server</code> survives when all connections to it are
-
 
450
     *         closed.
428
     */
451
     */
429
    public String getHostname(String server) {
452
    public boolean isPermanent(String server) {
430
        return server;
453
        return true;
431
    }
454
    }
432
 
455
 
433
    /**
456
    /**
434
     * Parse <code>url</code> to find info needed by {@link SQL_URL}.
457
     * Parse <code>url</code> to find info needed by {@link SQL_URL}.
435
     * 
458
     *