OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 180 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 180 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 25... Line 25...
25
import org.openconcerto.sql.utils.ChangeTable.DeferredClause;
25
import org.openconcerto.sql.utils.ChangeTable.DeferredClause;
26
import org.openconcerto.sql.utils.SQLUtils;
26
import org.openconcerto.sql.utils.SQLUtils;
27
import org.openconcerto.utils.CollectionUtils;
27
import org.openconcerto.utils.CollectionUtils;
28
import org.openconcerto.utils.CompareUtils;
28
import org.openconcerto.utils.CompareUtils;
29
import org.openconcerto.utils.ListMap;
29
import org.openconcerto.utils.ListMap;
30
import org.openconcerto.utils.NetUtils;
-
 
31
import org.openconcerto.utils.StringUtils;
30
import org.openconcerto.utils.StringUtils;
32
import org.openconcerto.utils.Tuple2;
31
import org.openconcerto.utils.Tuple2;
33
import org.openconcerto.utils.cc.ITransformer;
32
import org.openconcerto.utils.cc.ITransformer;
34
import org.openconcerto.utils.cc.IdentityHashSet;
33
import org.openconcerto.utils.cc.IdentityHashSet;
35
 
34
 
36
import java.io.File;
35
import java.io.File;
37
import java.io.FileFilter;
36
import java.io.FileFilter;
38
import java.io.IOException;
37
import java.io.IOException;
39
import java.math.BigDecimal;
38
import java.math.BigDecimal;
-
 
39
import java.math.RoundingMode;
40
import java.sql.DatabaseMetaData;
40
import java.sql.DatabaseMetaData;
41
import java.sql.SQLException;
41
import java.sql.SQLException;
42
import java.sql.Timestamp;
42
import java.sql.Timestamp;
43
import java.sql.Types;
43
import java.sql.Types;
44
import java.text.DateFormatSymbols;
44
import java.text.DateFormatSymbols;
Line 317... Line 317...
317
    private static SQLSyntax create(final SQLSystem sys, final DBSystemRoot sysRoot) {
317
    private static SQLSyntax create(final SQLSystem sys, final DBSystemRoot sysRoot) {
318
        final SQLSyntax res;
318
        final SQLSyntax res;
319
        if (sys == SQLSystem.POSTGRESQL)
319
        if (sys == SQLSystem.POSTGRESQL)
320
            res = new SQLSyntaxPG();
320
            res = new SQLSyntaxPG();
321
        else if (sys == SQLSystem.H2)
321
        else if (sys == SQLSystem.H2)
322
            res = new SQLSyntaxH2();
322
            res = SQLSyntaxH2.create(sysRoot);
323
        else if (sys == SQLSystem.MYSQL)
323
        else if (sys == SQLSystem.MYSQL)
324
            res = SQLSyntaxMySQL.create(sysRoot);
324
            res = SQLSyntaxMySQL.create(sysRoot);
325
        else if (sys == SQLSystem.MSSQL)
325
        else if (sys == SQLSystem.MSSQL)
326
            res = new SQLSyntaxMS();
326
            res = new SQLSyntaxMS();
327
        else
327
        else
Line 569... Line 569...
569
    }
569
    }
570
 
570
 
571
    public abstract boolean isDeadLockException(final SQLException exn);
571
    public abstract boolean isDeadLockException(final SQLException exn);
572
 
572
 
573
    /**
573
    /**
-
 
574
     * Whether the cause of the passed exception is a table not being found.
-
 
575
     * 
-
 
576
     * @param exn an exception.
-
 
577
     * @return <code>true</code> if the cause is a table not being found.
-
 
578
     */
-
 
579
    public abstract boolean isTableNotFoundException(final Exception exn);
-
 
580
 
-
 
581
    /**
-
 
582
     * How to set the amount of time any statement waits while attempting to acquire a lock.
-
 
583
     * <p>
-
 
584
     * NOTE : some systems only support seconds precision, in that case the amount will be
-
 
585
     * {@link RoundingMode#UP rounded up} so that the statement waits at least the passed amount.
-
 
586
     * </p>
-
 
587
     * <p>
-
 
588
     * <strong>Warning</strong> : some systems will wait 2 or 3 times the passed amount.
-
 
589
     * </p>
-
 
590
     * 
-
 
591
     * @param millis the number of milliseconds.
-
 
592
     * @return the SQL query.
-
 
593
     */
-
 
594
    public String getSetLockTimeoutQuery(final int millis) {
-
 
595
        return "SET lock_timeout " + millis;
-
 
596
    }
-
 
597
 
-
 
598
    public String getShowLockTimeoutQuery() {
-
 
599
        return "SELECT lock_timeout()";
-
 
600
    }
-
 
601
 
-
 
602
    /**
574
     * Something to be appended to CREATE TABLE statements, like "ENGINE = InnoDB".
603
     * Something to be appended to CREATE TABLE statements, like "ENGINE = InnoDB".
575
     * 
604
     * 
576
     * @return a String that need to be appended to CREATE TABLE statements.
605
     * @return a String that need to be appended to CREATE TABLE statements.
577
     */
606
     */
578
    public String getCreateTableSuffix() {
607
    public String getCreateTableSuffix() {
Line 708... Line 737...
708
                final int size = t.getSize();
737
                final int size = t.getSize();
709
                if (size < Integer.MAX_VALUE) {
738
                if (size < Integer.MAX_VALUE) {
710
                    sqlType = type + "(" + size + ")";
739
                    sqlType = type + "(" + size + ")";
711
                } else {
740
                } else {
712
                    Log.get().warning("Unbounded varchar for " + f.getSQLName());
741
                    Log.get().warning("Unbounded varchar for " + f.getSQLName());
713
                    // if (this.getSystem() == SQLSystem.MYSQL)
742
                    if (this.getSystem() == SQLSystem.MYSQL)
714
                    // throw new IllegalStateException("MySQL doesn't support unbounded varchar and
743
                        throw new IllegalStateException("MySQL doesn't support unbounded varchar and might truncate data if reducing size of " + f.getSQLName());
715
                    // might truncate data if reducing size of " + f.getSQLName());
-
 
716
                    // don't specify size
744
                    // don't specify size
717
                    sqlType = type;
745
                    sqlType = type;
718
                }
746
                }
719
            }
747
            }
720
        } else if (t.getJavaType() == BigDecimal.class) {
748
        } else if (t.getJavaType() == BigDecimal.class) {
Line 1028... Line 1056...
1028
        if (level != null)
1056
        if (level != null)
1029
            Log.get().log(level, "loading " + f + " into " + t.getSQLName() + "... ");
1057
            Log.get().log(level, "loading " + f + " into " + t.getSQLName() + "... ");
1030
        if (delete)
1058
        if (delete)
1031
            t.getBase().getDataSource().execute("DELETE FROM " + t.getSQLName().quote());
1059
            t.getBase().getDataSource().execute("DELETE FROM " + t.getSQLName().quote());
1032
        _loadData(f, t);
1060
        _loadData(f, t);
1033
        t.fireTableModified(SQLRow.NONEXISTANT_ID);
1061
        t.fireTableModified();
1034
        if (level != null)
1062
        if (level != null)
1035
            Log.get().log(level, "done loading " + f);
1063
            Log.get().log(level, "done loading " + f);
1036
    }
1064
    }
1037
 
1065
 
1038
    protected abstract void _loadData(File f, SQLTable t) throws IOException, SQLException;
1066
    protected abstract void _loadData(File f, SQLTable t) throws IOException, SQLException;
Line 1064... Line 1092...
1064
        this._storeData(t, f);
1092
        this._storeData(t, f);
1065
    }
1093
    }
1066
 
1094
 
1067
    protected abstract void _storeData(SQLTable t, File f) throws IOException;
1095
    protected abstract void _storeData(SQLTable t, File f) throws IOException;
1068
 
1096
 
1069
    /**
-
 
1070
     * Whether the passed server runs on this machine.
-
 
1071
     * 
-
 
1072
     * @param s the server to test.
-
 
1073
     * @return <code>true</code> if this jvm runs on the same machine than <code>s</code>.
-
 
1074
     */
-
 
1075
    protected boolean isServerLocalhost(SQLServer s) {
-
 
1076
        return NetUtils.isSelfAddr(s.getName());
-
 
1077
    }
-
 
1078
 
-
 
1079
    protected final void checkServerLocalhost(DBStructureItem<?> t) {
1097
    protected final void checkServerLocalhost(DBStructureItem<?> t) {
1080
        if (!this.isServerLocalhost(t.getServer()))
1098
        if (!t.getServer().isLocalhost())
1081
            throw new IllegalArgumentException("the server of " + t + " is not this computer: " + t.getServer());
1099
            throw new IllegalArgumentException("the server of " + t + " is not this computer: " + t.getServer());
1082
    }
1100
    }
1083
 
1101
 
1084
    /**
1102
    /**
1085
     * The function to return the character with the given ASCII code.
1103
     * The function to return the character with the given ASCII code.
Line 1532... Line 1550...
1532
            protected String asString(ChangeTable<?> ct, SQLName tableName) {
1550
            protected String asString(ChangeTable<?> ct, SQLName tableName) {
1533
                return "COMMENT ON TABLE " + tableName.quote() + " IS " + ct.getSyntax().quoteString(comment) + ";";
1551
                return "COMMENT ON TABLE " + tableName.quote() + " IS " + ct.getSyntax().quoteString(comment) + ";";
1534
            }
1552
            }
1535
        };
1553
        };
1536
    }
1554
    }
-
 
1555
 
-
 
1556
    /**
-
 
1557
     * The expression that returns the current session.
-
 
1558
     * 
-
 
1559
     * @return the expression to know the current session ID.
-
 
1560
     * @see #getSessionsQuery(DBSystemRoot, boolean)
-
 
1561
     */
-
 
1562
    public abstract String getSessionIDExpression();
-
 
1563
 
-
 
1564
    public final String getSessionsQuery(final DBSystemRoot sysRoot) {
-
 
1565
        return this.getSessionsQuery(sysRoot, true);
-
 
1566
    }
-
 
1567
 
-
 
1568
    /**
-
 
1569
     * Return a query to list the sessions in the passed system root.
-
 
1570
     * 
-
 
1571
     * @param sysRoot the system root.
-
 
1572
     * @param includeSelf <code>true</code> if the session executing the query should be returned.
-
 
1573
     * @return a query returning ID, QUERY and USER_NAME columns.
-
 
1574
     */
-
 
1575
    public abstract String getSessionsQuery(final DBSystemRoot sysRoot, final boolean includeSelf);
-
 
1576
 
-
 
1577
    /**
-
 
1578
     * Return a query to grant privileges on a table.
-
 
1579
     * 
-
 
1580
     * @param privileges which privileges, <code>null</code> for ALL, e.g. "SELECT".
-
 
1581
     * @param tableName which table.
-
 
1582
     * @param role which role, <code>null</code> for everyone, e.g. 'joe'.
-
 
1583
     * @return the query.
-
 
1584
     */
-
 
1585
    public String getGrantQuery(final List<String> privileges, final SQLName tableName, final String role) {
-
 
1586
        final String priv = privileges == null ? "ALL" : CollectionUtils.join(privileges, ", ");
-
 
1587
        return "GRANT " + priv + " ON " + tableName + " TO " + (role == null ? getAllUsersForGrant() : role);
-
 
1588
    }
-
 
1589
 
-
 
1590
    protected String getAllUsersForGrant() {
-
 
1591
        return "PUBLIC";
-
 
1592
    }
-
 
1593
 
-
 
1594
    public String getVersionFunction() {
-
 
1595
        return "version()";
-
 
1596
    }
-
 
1597
 
-
 
1598
    /**
-
 
1599
     * Return a query to allow/disallow connections to the passed database. I.e. allow exclusive
-
 
1600
     * access to the database.
-
 
1601
     * 
-
 
1602
     * @param sysRootName the database name.
-
 
1603
     * @param allow <code>true</code> to allow connections, <code>false</code> to disallow.
-
 
1604
     * @return the query, <code>null</code> if not supported.
-
 
1605
     */
-
 
1606
    public String getAllowConnectionsQuery(String sysRootName, final boolean allow) {
-
 
1607
        return null;
-
 
1608
    }
-
 
1609
 
-
 
1610
    public boolean isConnectionDisallowedException(final SQLException exn) {
-
 
1611
        throw new UnsupportedOperationException();
-
 
1612
    }
-
 
1613
 
-
 
1614
    public final String getSQLArray(final List<String> sqlExpressions) {
-
 
1615
        return this.getSQLArray(sqlExpressions, null);
-
 
1616
    }
-
 
1617
 
-
 
1618
    /**
-
 
1619
     * Return an SQL expression of an array.
-
 
1620
     * 
-
 
1621
     * @param sqlExpressions the items.
-
 
1622
     * @param type the component type, can be <code>null</code> to infer it from items (i.e. when
-
 
1623
     *        <code>sqlExpressions</code> isn't empty).
-
 
1624
     * @return an SQL expression.
-
 
1625
     */
-
 
1626
    public String getSQLArray(final List<String> sqlExpressions, final String type) {
-
 
1627
        throw new UnsupportedOperationException();
-
 
1628
    }
-
 
1629
 
-
 
1630
    public String getSQLArrayContains(final String arrayExpression, final String itemExpression) {
-
 
1631
        throw new UnsupportedOperationException();
-
 
1632
    }
-
 
1633
 
-
 
1634
    public String getSQLArrayLength(final String arrayExpression) {
-
 
1635
        throw new UnsupportedOperationException();
-
 
1636
    }
-
 
1637
 
-
 
1638
    public String getSQLArrayConcat(final String arrayExpression, final String array2Expression) {
-
 
1639
        throw new UnsupportedOperationException();
-
 
1640
    }
-
 
1641
 
-
 
1642
    public String getSQLArrayAppend(final String arrayExpression, final String itemExpression) {
-
 
1643
        throw new UnsupportedOperationException();
-
 
1644
    }
-
 
1645
 
-
 
1646
    public String getSQLArraySlice(final String arrayExpression, final String index1Expression, final String index2Expression) {
-
 
1647
        throw new UnsupportedOperationException();
-
 
1648
    }
1537
}
1649
}