Пример #1
0
        public string DataSourceName = ""; // datasource name


        /// <summary>
        /// Get the DataSourceMx associated with a schema name
        /// </summary>
        /// <param name="schemaName"></param>
        /// <returns></returns>

        public static DataSourceMx GetDataSourceForSchemaName(string schemaName)
        {
            DbSchemaMx schema = GetSchemaInfoFromName(schemaName);

            string dsName = schema?.DataSourceName;

            if (Lex.IsUndefined(dsName))
            {
                return(null);
            }

            if (!DataSourceMx.DataSources.ContainsKey(dsName))
            {
                return(null);                                         // unknown datasource
            }
            DataSourceMx ds = DataSourceMx.DataSources[dsName];

            return(ds);
        }
Пример #2
0
        public bool CanCreateTempTables = false;                            // true if the associated account can create temp tables

        /// <summary>
        /// Load metadata describing connections and associated schemas
        /// </summary>
        /// <param name="dsFileName"></param>
        /// <returns></returns>

        public static int LoadMetadata()
        {
            XmlAttributeCollection atts;

            // Get some inifile parameters first

            if (ServicesIniFile.IniFile == null)
            {
                throw new Exception("ServicesIniFile.IniFile is null");
            }

            SessionConnection.Pooling            = ServicesIniFile.ReadBool("Pooling", true);
            SessionConnection.MinPoolSize        = ServicesIniFile.ReadInt("MinPoolSize", 1);
            SessionConnection.IncrPoolSize       = ServicesIniFile.ReadInt("IncrPoolSize", 5);
            SessionConnection.MaxPoolSize        = ServicesIniFile.ReadInt("MaxPoolSize", 100);
            SessionConnection.DecrPoolSize       = ServicesIniFile.ReadInt("DecrPoolSize", 1);
            SessionConnection.ConnectionLifetime = ServicesIniFile.ReadInt("ConnectionLifetime", 0);

            string tok     = ServicesIniFile.Read("KeyListPredType", "Parameterized");
            int    enumInt = EnumUtil.Parse(typeof(KeyListPredTypeEnum), tok);

            if (enumInt >= 0)
            {
                DbCommandMx.DefaultKeyListPredType = (KeyListPredTypeEnum)enumInt;
            }

            DbCommandMx.DbFetchRowCount = ServicesIniFile.ReadInt("DbFetchRowCount", DbCommandMx.DbFetchRowCount);
            DbCommandMx.DbFetchSizeMax  = ServicesIniFile.ReadInt("DbFetchSizeMax", DbCommandMx.DbFetchSizeMax);

            DataSources = new Dictionary <string, DataSourceMx>(StringComparer.OrdinalIgnoreCase);
            Schemas     = new Dictionary <string, DbSchemaMx>(StringComparer.OrdinalIgnoreCase);

            string dsFileName = ServicesDirs.MetaDataDir + @"\" + "DataSources.xml";

            StreamReader sr  = new StreamReader(dsFileName);
            XmlDocument  doc = new XmlDocument();

            doc.Load(sr);
            XmlNode node = doc.FirstChild;

            while (node != null)
            {
                if (node.NodeType == XmlNodeType.Element)
                {
                    break;
                }
                node = node.NextSibling;
                if (node == null)
                {
                    throw new Exception("No initial element found");
                }
            }

            if (!Lex.Eq(node.Name, "DataSources"))
            {
                throw new Exception("Expected DataSources node: " + node.Name);
            }

            atts = node.Attributes;             // datasources attributes
            for (int i = 0; i < atts.Count; i++)
            {
                XmlNode att = atts.Item(i);

                if (Lex.Eq(att.Name, "DblinkConnName"))
                {
                }                                                           // obsolete key word

                else
                {
                    throw new Exception
                              ("Unexpected DataSources attribute: " + att.Name);
                }
            }

            node = node.FirstChild;
            while (node != null)
            {
                if (node.NodeType != XmlNodeType.Element)
                {
                    ;                                                       // ignore non-elements
                }
                else if (Lex.Eq(node.Name, "DataSource") ||                 // connection element
                         Lex.Eq(node.Name, "Connection"))
                {
                    DataSourceMx cd = new DataSourceMx();

                    atts = node.Attributes;
                    for (int i = 0; i < atts.Count; i++)
                    {
                        XmlNode att = atts.Item(i);

                        if (Lex.Eq(att.Name, "DatabaseType") || Lex.Eq(att.Name, "DbType"))
                        {
                            if (!Enum.TryParse <DatabaseType>(att.Value, true, out cd.DbType))
                            {
                                throw new Exception("Invalid Database type: " + att.Value);
                            }
                        }

                        else if (Lex.Eq(att.Name, "Name"))
                        {
                            cd.DataSourceName = att.Value.ToUpper().Trim();
                        }

                        else if (Lex.Eq(att.Name, "DatabaseName") || Lex.Eq(att.Name, "OracleName"))
                        {
                            cd.DatabaseLocator = att.Value.ToUpper().Trim();
                        }

                        else if (Lex.Eq(att.Name, "ConnectionString"))                         // keep case if connection string
                        {
                            cd.DatabaseLocator = att.Value.Trim();
                        }

                        else if (Lex.Eq(att.Name, "UserName"))
                        {
                            cd.UserName = att.Value.Trim();                             // keep case as is (Oracle 11g)
                        }
                        else if (Lex.Eq(att.Name, "Password"))
                        {
                            cd.Password = att.Value.Trim();                           // keep case as is (Oracle 11g)
                        }
                        else if (Lex.Eq(att.Name, "MdlInit"))                         // mdl initialization
                        {
                            cd.InitCommand = "select cdcaux.ctenvinit('" + att.Value.Trim() + "') from dual";
                        }

                        else if (Lex.Eq(att.Name, "InitCommand"))
                        {
                            cd.InitCommand = att.Value.Trim();
                        }

                        else if (Lex.Eq(att.Name, "KeyDataSource"))
                        {
                            bool.TryParse(att.Value.Trim(), out cd.IsKeyDataSource);
                        }

                        else if (Lex.Eq(att.Name, "CanCreateTempTables"))
                        {
                            bool.TryParse(att.Value.Trim(), out cd.CanCreateTempTables);
                        }

                        else if (Lex.Eq(att.Name, "AlwaysUseDbLink"))
                        {
                        }                                                                         // obsolete

                        else
                        {
                            throw new Exception
                                      ("Unexpected Connection attribute: " + att.Name);
                        }
                    }

                    if (cd.DataSourceName == "")
                    {
                        DebugLog.Message("Connection is missing name: " + cd.DatabaseLocator);
                    }
                    else if (DataSourceMx.DataSources.ContainsKey(cd.DataSourceName))
                    {
                        DebugLog.Message("Data source defined twice: " + cd.DataSourceName);
                    }
                    else
                    {
                        DataSourceMx.DataSources.Add(cd.DataSourceName, cd);
                    }
                }

                else if (Lex.Eq(node.Name, "SchemaToDataSource") ||                 // schema map element
                         Lex.Eq(node.Name, "SchemaToConnection"))
                {
                    DbSchemaMx schema = new DbSchemaMx();
                    atts = node.Attributes;
                    for (int i = 0; i < atts.Count; i++)
                    {
                        XmlNode att = atts.Item(i);

                        if (Lex.Eq(att.Name, "Schema"))
                        {
                            schema.Name = att.Value.ToUpper().Trim();
                        }

                        else if (Lex.Eq(att.Name, "AliasFor"))
                        {
                            schema.AliasFor = att.Value.ToUpper().Trim();
                        }

                        else if (Lex.Eq(att.Name, "Connection"))
                        {
                            schema.DataSourceName = att.Value.ToUpper().Trim();
                        }

                        else if (Lex.Eq(att.Name, "Label") || Lex.Eq(att.Name, "L"))
                        {
                            schema.Label = att.Value.Trim();
                        }

                        else
                        {
                            throw new Exception
                                      ("Unexpected Connection attribute: " + att.Name);
                        }
                    }

                    if (schema.Name == "")
                    {
                        throw new Exception("Missing schema name");
                    }
                    if (schema.DataSourceName == "")
                    {
                        throw new Exception("Missing data source/connection name");
                    }
                    Schemas[schema.Name] = schema;
                }

                else
                {
                    throw new Exception("Expected Connection or SchemaToDataSource element but saw " +
                                        node.Name);
                }

                node = node.NextSibling;
            }
            sr.Close();

            return(DataSources.Count);
        }