Java Code Examples for com.mysql.cj.util.StringUtils#splitDBdotName()

The following examples show how to use com.mysql.cj.util.StringUtils#splitDBdotName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private void determineParameterTypes() throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        java.sql.ResultSet paramTypesRs = null;

        try {
            //Bug#57022, we need to check for db.SPname notation first and pass on only SPname
            String procName = extractProcedureName();
            String quotedId = this.session.getIdentifierQuoteString();

            List<?> parseList = StringUtils.splitDBdotName(procName, "", quotedId, this.session.getServerSession().isNoBackslashEscapesSet());
            String tmpCatalog = "";
            //There *should* be 2 rows, if any.
            if (parseList.size() == 2) {
                tmpCatalog = (String) parseList.get(0);
                procName = (String) parseList.get(1);
            } else {
                //keep values as they are
            }

            java.sql.DatabaseMetaData dbmd = this.connection.getMetaData();

            boolean useCatalog = false;

            if (tmpCatalog.length() <= 0) {
                useCatalog = true;
            }

            paramTypesRs = dbmd.getProcedureColumns(useCatalog ? this.getCurrentCatalog() : tmpCatalog/* null */, null, procName, "%");

            boolean hasResults = false;
            try {
                if (paramTypesRs.next()) {
                    paramTypesRs.previous();
                    hasResults = true;
                }
            } catch (Exception e) {
                // paramTypesRs is empty, proceed with fake params. swallow, was expected 
            }
            if (hasResults) {
                convertGetProcedureColumnsToInternalDescriptors(paramTypesRs);
            } else {
                fakeParameterTypes(true);
            }
        } finally {
            SQLException sqlExRethrow = null;

            if (paramTypesRs != null) {
                try {
                    paramTypesRs.close();
                } catch (SQLException sqlEx) {
                    sqlExRethrow = sqlEx;
                }

                paramTypesRs = null;
            }

            if (sqlExRethrow != null) {
                throw sqlExRethrow;
            }
        }
    }
}
 
Example 2
@Override
public ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types) throws SQLException {
    if (catalog == null && this.nullCatalogMeansCurrent) {
        catalog = this.database;
    }

    catalog = this.pedantic ? catalog : StringUtils.unQuoteIdentifier(catalog, this.quotedId);

    if (tableNamePattern != null) {
        List<String> parseList = StringUtils.splitDBdotName(tableNamePattern, catalog, this.quotedId,
                this.session.getServerSession().isNoBackslashEscapesSet());
        //There *should* be 2 rows, if any.
        if (parseList.size() == 2) {
            tableNamePattern = parseList.get(1);
        }
    }

    java.sql.PreparedStatement pStmt = null;

    StringBuilder sqlBuf = new StringBuilder("SELECT TABLE_SCHEMA AS TABLE_CAT, NULL AS TABLE_SCHEM, TABLE_NAME, ");
    sqlBuf.append("CASE WHEN TABLE_TYPE='BASE TABLE' THEN CASE WHEN TABLE_SCHEMA = 'mysql' OR TABLE_SCHEMA = 'performance_schema' THEN 'SYSTEM TABLE' ");
    sqlBuf.append("ELSE 'TABLE' END WHEN TABLE_TYPE='TEMPORARY' THEN 'LOCAL_TEMPORARY' ELSE TABLE_TYPE END AS TABLE_TYPE, ");
    sqlBuf.append("TABLE_COMMENT AS REMARKS, NULL AS TYPE_CAT, NULL AS TYPE_SCHEM, NULL AS TYPE_NAME, NULL AS SELF_REFERENCING_COL_NAME, ");
    sqlBuf.append("NULL AS REF_GENERATION FROM INFORMATION_SCHEMA.TABLES WHERE");

    if (catalog != null) {
        sqlBuf.append("information_schema".equalsIgnoreCase(catalog) || "performance_schema".equalsIgnoreCase(catalog) || !StringUtils.hasWildcards(catalog)
                ? " TABLE_SCHEMA = ?" : " TABLE_SCHEMA LIKE ?");
    }

    if (tableNamePattern != null) {
        if (catalog != null) {
            sqlBuf.append(" AND");
        }
        sqlBuf.append(StringUtils.hasWildcards(tableNamePattern) ? " TABLE_NAME LIKE ?" : " TABLE_NAME = ?");
    }
    if (types != null && types.length > 0) {
        sqlBuf.append(" HAVING TABLE_TYPE IN (?,?,?,?,?)");
    }
    sqlBuf.append(" ORDER BY TABLE_TYPE, TABLE_SCHEMA, TABLE_NAME");
    try {
        pStmt = prepareMetaDataSafeStatement(sqlBuf.toString());

        int nextId = 1;
        if (catalog != null) {
            pStmt.setString(nextId++, catalog != null ? catalog : "%");
        }
        if (tableNamePattern != null) {
            pStmt.setString(nextId++, tableNamePattern);
        }

        if (types != null && types.length > 0) {
            for (int i = 0; i < 5; i++) {
                pStmt.setNull(nextId + i, MysqlType.VARCHAR.getJdbcType());
            }
            for (int i = 0; i < types.length; i++) {
                TableType tableType = TableType.getTableTypeEqualTo(types[i]);
                if (tableType != TableType.UNKNOWN) {
                    pStmt.setString(nextId++, tableType.getName());
                }
            }
        }

        ResultSet rs = executeMetadataQuery(pStmt);

        ((com.mysql.cj.jdbc.result.ResultSetInternalMethods) rs).setColumnDefinition(createTablesFields());

        return rs;
    } finally {
        if (pStmt != null) {
            pStmt.close();
        }
    }
}
 
Example 3
private void determineParameterTypes() throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        java.sql.ResultSet paramTypesRs = null;

        try {
            //Bug#57022, we need to check for db.SPname notation first and pass on only SPname
            String procName = extractProcedureName();
            String quotedId = this.session.getIdentifierQuoteString();

            List<?> parseList = StringUtils.splitDBdotName(procName, "", quotedId, this.session.getServerSession().isNoBackslashEscapesSet());
            String tmpCatalog = "";
            //There *should* be 2 rows, if any.
            if (parseList.size() == 2) {
                tmpCatalog = (String) parseList.get(0);
                procName = (String) parseList.get(1);
            } else {
                //keep values as they are
            }

            java.sql.DatabaseMetaData dbmd = this.connection.getMetaData();

            boolean useCatalog = false;

            if (tmpCatalog.length() <= 0) {
                useCatalog = true;
            }

            paramTypesRs = dbmd.getProcedureColumns(useCatalog ? this.getCurrentCatalog() : tmpCatalog/* null */, null, procName, "%");

            boolean hasResults = false;
            try {
                if (paramTypesRs.next()) {
                    paramTypesRs.previous();
                    hasResults = true;
                }
            } catch (Exception e) {
                // paramTypesRs is empty, proceed with fake params. swallow, was expected 
            }
            if (hasResults) {
                convertGetProcedureColumnsToInternalDescriptors(paramTypesRs);
            } else {
                fakeParameterTypes(true);
            }
        } finally {
            SQLException sqlExRethrow = null;

            if (paramTypesRs != null) {
                try {
                    paramTypesRs.close();
                } catch (SQLException sqlEx) {
                    sqlExRethrow = sqlEx;
                }

                paramTypesRs = null;
            }

            if (sqlExRethrow != null) {
                throw sqlExRethrow;
            }
        }
    }
}
 
Example 4
@Override
public ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types) throws SQLException {
    if (catalog == null && this.nullCatalogMeansCurrent) {
        catalog = this.database;
    }

    catalog = this.pedantic ? catalog : StringUtils.unQuoteIdentifier(catalog, this.quotedId);

    if (tableNamePattern != null) {
        List<String> parseList = StringUtils.splitDBdotName(tableNamePattern, catalog, this.quotedId,
                this.session.getServerSession().isNoBackslashEscapesSet());
        //There *should* be 2 rows, if any.
        if (parseList.size() == 2) {
            tableNamePattern = parseList.get(1);
        }
    }

    java.sql.PreparedStatement pStmt = null;

    StringBuilder sqlBuf = new StringBuilder("SELECT TABLE_SCHEMA AS TABLE_CAT, NULL AS TABLE_SCHEM, TABLE_NAME, ");
    sqlBuf.append("CASE WHEN TABLE_TYPE='BASE TABLE' THEN CASE WHEN TABLE_SCHEMA = 'mysql' OR TABLE_SCHEMA = 'performance_schema' THEN 'SYSTEM TABLE' ");
    sqlBuf.append("ELSE 'TABLE' END WHEN TABLE_TYPE='TEMPORARY' THEN 'LOCAL_TEMPORARY' ELSE TABLE_TYPE END AS TABLE_TYPE, ");
    sqlBuf.append("TABLE_COMMENT AS REMARKS, NULL AS TYPE_CAT, NULL AS TYPE_SCHEM, NULL AS TYPE_NAME, NULL AS SELF_REFERENCING_COL_NAME, ");
    sqlBuf.append("NULL AS REF_GENERATION FROM INFORMATION_SCHEMA.TABLES");

    if (catalog != null || tableNamePattern != null) {
        sqlBuf.append(" WHERE");
    }

    if (catalog != null) {
        sqlBuf.append("information_schema".equalsIgnoreCase(catalog) || "performance_schema".equalsIgnoreCase(catalog) || !StringUtils.hasWildcards(catalog)
                ? " TABLE_SCHEMA = ?" : " TABLE_SCHEMA LIKE ?");
    }

    if (tableNamePattern != null) {
        if (catalog != null) {
            sqlBuf.append(" AND");
        }
        sqlBuf.append(StringUtils.hasWildcards(tableNamePattern) ? " TABLE_NAME LIKE ?" : " TABLE_NAME = ?");
    }
    if (types != null && types.length > 0) {
        sqlBuf.append(" HAVING TABLE_TYPE IN (?,?,?,?,?)");
    }
    sqlBuf.append(" ORDER BY TABLE_TYPE, TABLE_SCHEMA, TABLE_NAME");
    try {
        pStmt = prepareMetaDataSafeStatement(sqlBuf.toString());

        int nextId = 1;
        if (catalog != null) {
            pStmt.setString(nextId++, catalog != null ? catalog : "%");
        }
        if (tableNamePattern != null) {
            pStmt.setString(nextId++, tableNamePattern);
        }

        if (types != null && types.length > 0) {
            for (int i = 0; i < 5; i++) {
                pStmt.setNull(nextId + i, MysqlType.VARCHAR.getJdbcType());
            }
            for (int i = 0; i < types.length; i++) {
                TableType tableType = TableType.getTableTypeEqualTo(types[i]);
                if (tableType != TableType.UNKNOWN) {
                    pStmt.setString(nextId++, tableType.getName());
                }
            }
        }

        ResultSet rs = executeMetadataQuery(pStmt);

        ((com.mysql.cj.jdbc.result.ResultSetInternalMethods) rs).setColumnDefinition(createTablesFields());

        return rs;
    } finally {
        if (pStmt != null) {
            pStmt.close();
        }
    }
}
 
Example 5
/**
 * Tests sanitation and SplitDBdotName
 * 
 * @throws Exception
 *             if an error occurs
 */
public void testSplit() throws Exception {
    String src = null;
    String resString = null;
    List<String> results = new ArrayList<>();

    //Test 1.1, weird DB.SP name
    src = "`MyDatabase 1.0.1.0`.`Proc 1.v1`";
    resString = StringUtils.sanitizeProcOrFuncName(src);
    if ((resString != null)) {
        results = StringUtils.splitDBdotName(resString, null, "`", true);
        assertEquals(results.get(0), "MyDatabase 1.0.1.0");
        assertEquals(results.get(1), "Proc 1.v1");
    } else {
        fail("Test 1.1 returned null resString");
    }

    //Test 1.2, toggle isNoBslashEscSet
    src = "`MyDatabase 1.0.1.0`.`Proc 1.v1`";
    resString = StringUtils.sanitizeProcOrFuncName(src);
    if ((resString != null)) {
        results = StringUtils.splitDBdotName(resString, null, "`", false);
        assertEquals(results.get(0), "MyDatabase 1.0.1.0");
        assertEquals(results.get(1), "Proc 1.v1");
    } else {
        fail("Test 1.2 returned null resString");
    }

    //Test 2.1, weird SP name, no DB parameter
    src = "`Proc 1.v1`";
    resString = StringUtils.sanitizeProcOrFuncName(src);
    if ((resString != null)) {
        results = StringUtils.splitDBdotName(resString, null, "`", true);
        assertEquals(results.get(0), null);
        assertEquals(results.get(1), "Proc 1.v1");
    } else {
        fail("Test 2.1 returned null resString");
    }

    //Test 2.2, toggle isNoBslashEscSet
    src = "`Proc 1.v1`";
    resString = StringUtils.sanitizeProcOrFuncName(src);
    if ((resString != null)) {
        results = StringUtils.splitDBdotName(resString, null, "`", false);
        assertEquals(results.get(0), null);
        assertEquals(results.get(1), "Proc 1.v1");
    } else {
        fail("Test 2.2 returned null resString");
    }
}