public DatabaseConnection(DbConnection connection, SqlDBCredentials credentials)
 {
     m_Connection              = connection;
     m_LastUse                 = DateTime.UtcNow;
     m_nConnectionRetries      = credentials.ConnectionRetries;
     m_ConnectionRetryInterval = credentials.ConnectionRetryInterval;
 }
        public SqlDBCredentials Clone(String newDefaultSchema = null)
        {
            SqlDBCredentials creds = (SqlDBCredentials)this.MemberwiseClone();

            if (newDefaultSchema != null)
            {
                creds.Schema = newDefaultSchema;
            }
            return(creds);
        }
        public static new DatabaseConnection CreateConnection(SqlDBCredentials credentials)
        {
            int    port = 3306;
            string host = credentials.Host;

            if (credentials.IsSSH)
            {
                SSHTunnels.EnsureTunnelExists(credentials);
                port = credentials.SSHCredentials.LocalTunnelPort;
                if (credentials.SSHCredentials.IsTunnel)
                {
                    host = IPAddress.Loopback.ToString();
                }
                else
                {
                    host = credentials.SSHCredentials.TunnelHost;
                }
            }

            String connectString;

            if (String.IsNullOrEmpty(credentials.Charset))
            {
                connectString = String.Format("Database={0};Data Source={1};User Id={2};Password=\x22{3}\x22;allow user variables=true;allow Zero Datetime=true;Port={4};{5};default command timeout=60",
                                              credentials.Schema,
                                              host,
                                              credentials.UserName,
                                              credentials.Password,
                                              port,
                                              credentials.Options);
            }
            else
            {
                connectString = String.Format("Database={0};Data Source={1};User Id={2};Password=\x22{3}\x22;charset={4};allow user variables=true;allow Zero Datetime=true;Port={5};{6}",
                                              credentials.Schema,
                                              host,
                                              credentials.UserName,
                                              credentials.Password,
                                              credentials.Charset,
                                              port,
                                              credentials.Options);
            }

            DatabaseConnection connection = null;

            try
            {
                connection = new DatabaseConnection(new MySqlConnection(connectString), credentials);
            }
            catch (Exception e)
            {
                Console.WriteLine(ThreadBase.GetFormattedStackTrace(e));
            }
            return(connection);
        }
Esempio n. 4
0
        public static DatabaseConnection CreateConnection(SqlDBCredentials credentials)
        {
            String strConnect = String.Format("Driver={{{0}}};UID={1};PWD={2};Server={3};Database={4};",
                                              credentials.Driver,
                                              credentials.UserName,
                                              credentials.Password,
                                              credentials.Host,
                                              credentials.Schema);

            return(new DatabaseConnection(new OdbcConnection(strConnect), credentials));
        }
        public static SqlConnectionPool Instance(SqlDBCredentials credentials)
        {
            SqlConnectionPool retVal = s_PoolList.Find(credentials);

            if (retVal == null)
            {
                retVal = new SqlConnectionPool(credentials);
                s_PoolList.Add(retVal);
            }
            return(retVal);
        }
            public SqlConnectionPool Find(SqlDBCredentials credentials)
            {
                SqlConnectionPool retVal = null;

                foreach (SqlConnectionPool pool in this)
                {
                    if (pool.GetCredentials().Equals(credentials))
                    {
                        retVal = pool;
                        break;
                    }
                }
                return(retVal);
            }
        public override bool Equals(object obj)
        {
            if (obj is SqlDBCredentials == false)
            {
                throw new InvalidCastException("Other object is not SqlDBCredentials");
            }
            SqlDBCredentials credentials = obj as SqlDBCredentials;

            return(credentials.Schema == Schema &&
                   credentials.Host == Host &&
                   credentials.Driver == Driver &&
                   credentials.UserName == UserName &&
                   credentials.Password == Password);
        }
Esempio n. 8
0
        public static DataSourceType GetDriverType(SqlDBCredentials credentials)
        {
            DataSourceType type = DataSourceType.Unknown;

            foreach (KeyValuePair <String, DataSourceType> kvp in m_DriverRegex)
            {
                if (Regex.IsMatch(credentials.Driver, kvp.Key, RegexOptions.IgnoreCase))
                {
                    type = kvp.Value;
                    break;
                }
            }
            return(type);
        }
        public static bool TryCreate <T>(SqlDBCredentials credentials, out T outVal, bool checkVersion = true, bool checkDeclarations = false, SoftwareVersion version = null, Type defaultDSType = null, bool readOnly = false)
        {
            bool retVal = false;

            outVal = default(T);
            try
            {
                outVal = DataSourceFactory.Create <T>(credentials, checkVersion, checkDeclarations, version, defaultDSType);
                retVal = true;
            }
            catch
            {
            }
            return(retVal);
        }
Esempio n. 10
0
        public static bool TryCreateDynamic(String assemblyName, String className, SqlDBCredentials credentials, out Object outVal, bool checkDeclarations = false, SoftwareVersion version = null, Type defaultDSType = null, bool readOnly = false)
        {
            bool retVal = false;

            outVal = null;
            try
            {
                outVal = DataSourceFactory.CreateDynamic(assemblyName, className, credentials, checkDeclarations, version, defaultDSType);
                retVal = true;
            }
            catch
            {
            }
            return(retVal);
        }
Esempio n. 11
0
        protected CachedDataSource(SqlDBCredentials credentials, ObjectCache cacheProvider, string regionName)
            : base(credentials)
        {
            if (cacheProvider == null)
            {
                throw new ArgumentNullException("cacheProvider");
            }

            if (cacheProvider is MemoryCache)
            {
                regionName = null;
            }

            _cacheProvider = cacheProvider;
            _regionName    = regionName;
        }
Esempio n. 12
0
        protected DataSourceBase(SqlDBCredentials sqlCredentials)
        {
            _internalDatabaseType = InternalDatabaseType.Unknown;
            Credentials           = sqlCredentials;
            m_DB = SqlConnectionPool.Instance(sqlCredentials).GetDataSource();

            if (m_DB == null)
            {
                throw new DataSourceException("Could not instantiate {0} with the following credentials: {1}", GetType(), sqlCredentials);
            }

            if (CheckDeclarations)
            {
                ValidateMethods(GetType());
            }

            LoadCachedTypeInformation();
        }
Esempio n. 13
0
            public T Get <T>(SqlDBCredentials credentials, SoftwareVersion version = null)
            {
                T retVal = default(T);
                CredentialLookup lkp;

                if (_credentialLookup.TryGetValue(typeof(T), out lkp))
                {
                    if (version == null)
                    {
                        version = new SoftwareVersion();
                    }

                    String key = credentials.ToParsableString() + ";DBVER=" + version.ToString();
                    if (lkp.ContainsKey(key))
                    {
                        retVal = (T)lkp[key];
                    }
                }
                return(retVal);
            }
Esempio n. 14
0
            public Object GetByType(Type t, SqlDBCredentials credentials, SoftwareVersion version = null)
            {
                Object           retVal = null;
                CredentialLookup lkp;

                if (_credentialLookup.TryGetValue(t, out lkp))
                {
                    if (version == null)
                    {
                        version = new SoftwareVersion();
                    }

                    String key = credentials.ToParsableString() + ";DBVER=" + version.ToString();
                    if (lkp.ContainsKey(key))
                    {
                        retVal = lkp[key];
                    }
                }
                return(retVal);
            }
Esempio n. 15
0
        public static DBResult GetDatabaseVersion(SqlDBCredentials credentials, out SoftwareVersion version, out InternalDatabaseType databaseType)
        {
            DBResult retVal = new DBResult(DBResult.Result.Success);

            try
            {
                _databaseVersionLock.Lock();
                version      = null;
                databaseType = InternalDatabaseType.Unknown;
                String indexKey = credentials.ToParsableString();
                if (!_databaseVersionLookup.TryGetValue(indexKey, out version))
                {
                    ISqlDataSource     ds = SqlConnectionPool.Instance(credentials).GetDataSource();
                    DatabaseDataReader reader;

                    QueryString sql = ds.FormatTrusted("SELECT * FROM I.seq where name in ('{0}', '{1}')", KEY_DB_TYPE, KEY_SCHEMA_VER);
                    retVal = ds.Query(sql, out reader);
                    if (retVal.ResultCode == DBResult.Result.Success && reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            String key = DBUtil.GetString(reader[COL_KEY]);
                            if (key == KEY_SCHEMA_VER)
                            {
                                version = DBUtil.GetSoftwareVersion(reader[COL_VALUE]);
                            }
                            else if (key == KEY_DB_TYPE)
                            {
                                databaseType = (InternalDatabaseType)DBUtil.GetInt32(reader[COL_VALUE]);
                            }
                        }
                        _databaseVersionLookup.Add(indexKey, version);
                    }
                }
            }
            finally
            {
                _databaseVersionLock.Unlock();
            }
            return(retVal);
        }
Esempio n. 16
0
            public T Set <T>(SqlDBCredentials credentials, SoftwareVersion version, T obj)
            {
                CredentialLookup lkp;

                if (!_credentialLookup.TryGetValue(typeof(T), out lkp))
                {
                    lkp = new CredentialLookup();
                    _credentialLookup.Add(typeof(T), lkp);
                }
                String key = credentials.ToParsableString() + ";DBVER=" + version.ToString();

                if (lkp.ContainsKey(key))
                {
                    lkp[key] = obj;
                }
                else
                {
                    lkp.Add(key, obj);
                }
                return(obj);
            }
Esempio n. 17
0
 public void Init(SqlDBCredentials creds)
 {
     m_Credentials   = creds;
     m_sqlDataSource = SqlConnectionPool.Instance(m_Credentials).GetDataSource();
 }
Esempio n. 18
0
        public static Object CreateDynamic(String assemblyName, String className, SqlDBCredentials credentials, bool checkDeclarations = false, SoftwareVersion version = null, Type defaultDSType = null, bool readOnly = false)
        {
            /** make a copy of these */
            credentials = credentials.Clone();

            /** ensure version is valid */
            if (version == null)
            {
                version = new SoftwareVersion();
            }

            InternalDatabaseType databaseType = InternalDatabaseType.Unknown;


            Object retVal = null;

            try
            {
                _instanceLock.Lock();
                SoftwareVersion initialVersion = version;
                Assembly        asm            = null;
                Type            t = null;
                try
                {
                    asm = Assembly.LoadFrom(assemblyName);
                    t   = asm.GetType(className);
                }
                catch (Exception e)
                {
                    throw new DataSourceException("DataSource Factory unable to load Datasource from Assembly: {0} - {1}", assemblyName, e);
                }

                _InstanceLookup.GetByType(t, credentials, version);

                // No cached instance of the proper datasource for these credentials about. Try and find one.
                if (retVal == null)
                {
                    // Now we get the database version from the lookup or database
                    if (version == SoftwareVersion.Empty && !(GetDatabaseVersion(credentials, out version, out databaseType).ResultCode == DBResult.Result.Success))
                    {
                        throw new DataSourceException("DataSource Factory unable to obtain version from Database: {0} (2)", credentials.Host);
                    }


                    //1. Get specified version of T. Routine will Start at T and look at descendants, caching results.
                    Type type = _TypeLookup.GetByType(t, version);

                    //1a. Throw exception if T has no version decorations
                    if (type == null)
                    {
                        if (defaultDSType == null)
                        {
                            throw new DataSourceException("Unable to find  version {0} of DataSource {1}", version, t);
                        }
                        else
                        {
                            type = defaultDSType;
                        }
                    }

                    bool saveDeclarationCheck = false;
                    /** save global declaration check flag */
                    if (type.IsSubclassOf(typeof(DataSourceBase)))
                    {
                        saveDeclarationCheck             = DataSourceBase.CheckDeclarations;
                        DataSourceBase.CheckDeclarations = checkDeclarations;
                    }

                    /** Create instance with proper parameters */
                    retVal = Activator.CreateInstance(type, credentials);
                    _InstanceLookup.SetByType(t, credentials, version, retVal);
                    if (initialVersion != version)
                    {
                        _InstanceLookup.SetByType(t, credentials, initialVersion, retVal);
                    }

                    /** restore global declaration check flag */
                    if (type.IsSubclassOf(typeof(DataSourceBase)))
                    {
                        DataSourceBase.CheckDeclarations = saveDeclarationCheck;
                    }

                    if (retVal is DataSourceBase)
                    {
                        ((DataSourceBase)(Object)retVal).ReadOnly     = readOnly;
                        ((DataSourceBase)(Object)retVal).DatabaseType = databaseType;
                    }
                }
            }
            catch (Exception e)
            {
                Log.SysLogText(LogLevel.ERROR, "DataSourceFactory Exception: {0}", e.Message);
                if (e.InnerException != null)
                {
                    Log.SysLogText(LogLevel.ERROR, "   Inner Exception: {0}", e.InnerException.Message);
                }

                /** rethrow */
                throw e;
            }
            finally
            {
                _instanceLock.Unlock();
            }
            return(retVal);
        }
Esempio n. 19
0
 public static bool TryParse(String str, out SqlDBCredentials creds)
 {
     creds = new SqlDBCredentials();
     return(creds.TryParse(str));
 }
Esempio n. 20
0
 public SqlConnectionPool(SqlDBCredentials credentials)
 {
     _credentials = credentials.Clone();
     _lastConnectionCleanupTime = DateTime.UtcNow;
 }
Esempio n. 21
0
 /**
  * Instance(SqlDBCredentials)
  * Gets the Connection Pool correspondant to the credentials passed in
  */
 public static void SetDefaultCredentials(SqlDBCredentials credentials)
 {
     s_DefaultCredentials = credentials.Clone();
 }