Ejemplo n.º 1
0
        public DataTable GetOleDbSchemaTable(Guid schema, object[] restrictions)
        {
            if (State != ConnectionState.Open)
            {
                throw ExceptionHelper.ConnectionNotOpened("GetOleDbSchemaTable", State.ToString());
            }

            try
            {
                string[] fixedRestrictions = new string[4];
                if (restrictions != null)
                {
                    if (restrictions.Length > 4)
                    {
                        throw new OleDbException("The parameter is incorrect", null, this);
                    }

                    for (int i = 0, count = restrictions.Length; i < count; i++)
                    {
                        if (restrictions[i] != null)
                        {
                            if (!(restrictions[i] is string))
                            {
                                throw new OleDbException("The parameter is incorrect", null, this);
                            }

                            fixedRestrictions[i] = (string)restrictions[i];
                        }
                    }
                }

                DataTable schemaTable = new DataTable("Tables");
                schemaTable.Columns.Add("TABLE_CATALOG");
                schemaTable.Columns.Add("TABLE_SCHEMA");
                schemaTable.Columns.Add("TABLE_NAME");
                schemaTable.Columns.Add("TABLE_TYPE");
                schemaTable.Columns.Add("TABLE_GUID");
                schemaTable.Columns.Add("DESCRIPTION");
                schemaTable.Columns.Add("TABLE_PROPID");
                schemaTable.Columns.Add("DATE_CREATED");
                schemaTable.Columns.Add("DATE_MODIFIED");

                java.sql.ResultSet tableRes = JdbcConnection.getMetaData().getTables(
                    fixedRestrictions[0],
                    fixedRestrictions[1],
                    fixedRestrictions[2],
                    new string[] { fixedRestrictions[3] });

                try
                {
                    while (tableRes.next())
                    {
                        DataRow row = schemaTable.NewRow();
                        row["TABLE_CATALOG"] = tableRes.getString("TABLE_CAT");
                        row["TABLE_SCHEMA"]  = tableRes.getString("TABLE_SCHEM");
                        row["TABLE_NAME"]    = tableRes.getString("TABLE_NAME");
                        row["TABLE_TYPE"]    = tableRes.getString("TABLE_TYPE");
                        row["DESCRIPTION"]   = tableRes.getString("REMARKS");

                        schemaTable.Rows.Add(row);
                    }
                }
                finally
                {
                    tableRes.close();
                }

                return(schemaTable);
            }
            catch (SQLException e)
            {
                throw CreateException(e);
            }
        }
        internal ArrayList GetProcedureColumns(String procedureString, AbstractDbCommand command)
        {
            ArrayList col = new ArrayList();

            try
            {
                ObjectNameResolver[] nameResolvers = SyntaxPatterns;
                java.sql.ResultSet   res           = null;
                string catalog = null;
                string schema  = null;
                string spname  = null;

                java.sql.DatabaseMetaData metadata = JdbcConnection.getMetaData();
                bool storesUpperCaseIdentifiers    = false;
                bool storesLowerCaseIdentifiers    = false;
                try
                {
                    storesUpperCaseIdentifiers = metadata.storesUpperCaseIdentifiers();
                    storesLowerCaseIdentifiers = metadata.storesLowerCaseIdentifiers();
                }
                catch (SQLException e)
                {
                    // suppress
                }

                for (int i = 0; i < nameResolvers.Length; i++)
                {
                    ObjectNameResolver nameResolver = nameResolvers[i];
                    Match match = nameResolver.Match(procedureString);

                    if (match.Success)
                    {
                        spname  = ObjectNameResolver.GetName(match);
                        schema  = ObjectNameResolver.GetSchema(match);
                        catalog = ObjectNameResolver.GetCatalog(match);

                        // make all identifiers uppercase or lowercase according to database metadata
                        if (storesUpperCaseIdentifiers)
                        {
                            spname  = (spname.Length > 0) ? spname.ToUpper() : null;
                            schema  = (schema.Length > 0) ? schema.ToUpper() : null;
                            catalog = (catalog.Length > 0) ? catalog.ToUpper() : null;
                        }
                        else if (storesLowerCaseIdentifiers)
                        {
                            spname  = (spname.Length > 0) ? spname.ToLower() : null;
                            schema  = (schema.Length > 0) ? schema.ToLower() : null;
                            catalog = (catalog.Length > 0) ? catalog.ToLower() : null;
                        }
                        else
                        {
                            spname  = (spname.Length > 0) ? spname : null;
                            schema  = (schema.Length > 0) ? schema : null;
                            catalog = (catalog.Length > 0) ? catalog : null;
                        }

                        // catalog from db is always in correct caps
                        if (catalog == null)
                        {
                            catalog = JdbcConnection.getCatalog();
                        }

                        try
                        {
                            // always get the first procedure that db returns
                            res = metadata.getProcedures(catalog, schema, spname);
                            if (res.next())
                            {
                                catalog = res.getString(1);
                                schema  = res.getString(2);
                                spname  = res.getString(3);
                                break;
                            }

                            spname = null;
                        }
                        catch // suppress exception
                        {
                            return(null);
                        }
                        finally
                        {
                            if (res != null)
                            {
                                res.close();
                            }
                        }
                    }
                }

                if (spname == null || spname.Length == 0)
                {
                    return(null);
                }

                try
                {
                    // get procedure columns based o  procedure metadata
                    res = metadata.getProcedureColumns(catalog, schema, spname, null);
                    while (res.next())
                    {
                        // since there is still a possibility that some of the parameters to getProcedureColumn were nulls,
                        // we need to filter the results with strict matching
                        if ((res.getString(1) != catalog) || (res.getString(2) != schema) || (res.getString(3) != spname))
                        {
                            continue;
                        }

                        AbstractDbParameter parameter = (AbstractDbParameter)command.CreateParameter();

                        parameter.SetParameterName(res);
                        parameter.SetParameterDbType(res);
                        parameter.SetSpecialFeatures(res);

                        //get parameter direction
                        short direction = res.getShort("COLUMN_TYPE");
                        if (direction == 1) //DatabaseMetaData.procedureColumnIn
                        {
                            parameter.Direction = ParameterDirection.Input;
                        }
                        else if (direction == 2) //DatabaseMetaData.procedureColumnInOut
                        {
                            parameter.Direction = ParameterDirection.InputOutput;
                        }
                        else if (direction == 4) //DatabaseMetaData.procedureColumnOut
                        {
                            parameter.Direction = ParameterDirection.Output;
                        }
                        else if (direction == 5) //DatabaseMetaData.procedureColumnReturn
                        {
                            parameter.Direction = ParameterDirection.ReturnValue;
                        }

                        //get parameter precision and scale
                        parameter.SetParameterPrecisionAndScale(res);

                        parameter.SetParameterSize(res);
                        parameter.SetParameterIsNullable(res);

                        col.Add(parameter);
                    }
                }
                finally
                {
                    if (res != null)
                    {
                        res.close();
                    }
                }
            }
            catch (Exception e)
            {
                //supress
#if DEBUG
                Console.WriteLine("Exception catched at AbstractDBConnection.GetProcedureColumns() : {0}\n{1}\n{2}", e.GetType().FullName, e.Message, e.StackTrace);
#endif
            }
            return(col);
        }