metamodel-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "zhaoweijing (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (METAMODEL-156) JdbcDataContext.getDefaultSchemaName return SchemaName error
Date Mon, 06 Jul 2015 05:05:04 GMT

    [ https://issues.apache.org/jira/browse/METAMODEL-156?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14614542#comment-14614542
] 

zhaoweijing commented on METAMODEL-156:
---------------------------------------

I solve this problem by schema length, the code is as follows. If you have a better solution,
it is wonderful.


    public String getDefaultSchemaName() {
        // Use a boolean to check if the result has been
        // found, because a schema name can actually be
        // null (for example in the case of Firebird
        // databases).
        boolean found = false;
        String result = null;
        List<String> result2 = new ArrayList<String>();
        String[] schemaNames = getSchemaNames();

        // First strategy: If there's only one schema available, that must
        // be it
        if (schemaNames.length == 1) {
            result = schemaNames[0];
            found = true;
        }

        if (!found) {
            Connection connection = getConnection();
            try {
                DatabaseMetaData metaData = connection.getMetaData();

                // Second strategy: Find default schema name by examining the
                // URL
                if (!found) {
                    String url = metaData.getURL();
                    if (url != null && url.length() > 0) {
                        if (schemaNames.length > 0) {
                            StringTokenizer st = new StringTokenizer(url, "/\\:");
                            int tokenCount = st.countTokens();
                            if (tokenCount > 0) {
                                for (int i = 1; i < tokenCount; i++) {
                                    st.nextToken();
                                }
                                String lastToken = st.nextToken();

                                for (int i = 0; i < schemaNames.length ; i++) {
                                    String schemaName = schemaNames[i];
                                    if (lastToken.indexOf(schemaName) != -1) {
                                        result = schemaName;
                                        result2.add(result);
                                        found = true;
                                    }
                                }
                                //bug: baseFW   baseFWTest  solve base name is the same 
                                if(result2.size()>1)
                                {
                                	result = "";
                                	for (String str : result2) {
										if(str.length()>result.length())
										{
											result = str;
										}
									}
                                }
                                //bug solve end
                            }
                        }
                    }
                }

                // Third strategy: Check for schema equal to username
                if (!found) {
                    String username = metaData.getUserName();
                    if (username != null) {
                        for (int i = 0; i < schemaNames.length && !found; i++)
{
                            if (username.equalsIgnoreCase(schemaNames[i])) {
                                result = schemaNames[i];
                                found = true;
                            }
                        }
                    }
                }

            } catch (SQLException e) {
                throw JdbcUtils.wrapException(e, "determine default schema name");
            } finally {
                closeIfNescesary(connection);
            }

            // Fourth strategy: Find default schema name by vendor-specific
            // hacks
            if (!found) {
                if (DATABASE_PRODUCT_POSTGRESQL.equalsIgnoreCase(_databaseProductName)) {
                    if (_catalogName == null) {
                        result = "public";
                    } else {
                        result = _catalogName;
                    }
                    found = true;
                }
                if (DATABASE_PRODUCT_HSQLDB.equalsIgnoreCase(_databaseProductName)) {
                    for (int i = 0; i < schemaNames.length && !found; i++) {
                        String schemaName = schemaNames[i];
                        if ("PUBLIC".equals(schemaName)) {
                            result = schemaName;
                            found = true;
                            break;
                        }
                    }
                }
                if (DATABASE_PRODUCT_SQLSERVER.equals(_databaseProductName)) {
                    for (int i = 0; i < schemaNames.length && !found; i++) {
                        String schemaName = schemaNames[i];
                        if ("dbo".equals(schemaName)) {
                            result = schemaName;
                            found = true;
                            break;
                        }
                    }
                }
            }
        }
        return result;
    }



> JdbcDataContext.getDefaultSchemaName  return SchemaName error
> -------------------------------------------------------------
>
>                 Key: METAMODEL-156
>                 URL: https://issues.apache.org/jira/browse/METAMODEL-156
>             Project: Apache MetaModel
>          Issue Type: Bug
>    Affects Versions: 4.2.0-incubating, 4.3.0-incubating, 4.3.2
>         Environment: windows 7 64  bit eclipse 4.2 luna
>            Reporter: zhaoweijing
>              Labels: starter
>             Fix For: 4.2.0-incubating, 4.3.0-incubating, 4.3.2
>
>
> my database has three mysql databases, for example, baseFW, baseFW_test, test. when my
mysql url is : http:///3306?baseFW_test, and i want get baseFW_test jdbccontext,but i will
get baseFW jdbccontext. because indexOf("baseFW") >0 ,so return first baseFW.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message