Ejemplo n.º 1
0
 public DbAdapter(DbDriverType driverType, IDbConnectionInfo connectionInfo)
 {
     Driver = DbDriverFactory.GetInstance(driverType);
     ConnectionInfo = connectionInfo;
     Connection = Driver.BuildConnection(ConnectionInfo);
     OpenConnection();
 }
Ejemplo n.º 2
0
    public void Run(IDbConnectionInfo connectionInfo)
    {
      var db = MockDatabaseProvider.Open(connectionInfo) as TestMockDatabase;
      if (db == null) throw new ArgumentException("Could not open mock database from supplied connectionInfo", "connectionInfo");

      db.Table.Add(new TestMockDatabase.Row {Id = 3, RowName = "Three"});
    }
Ejemplo n.º 3
0
 public DbAdapter(DbDriverType driverType, IDbConnectionInfo connectionInfo)
 {
     Driver         = DbDriverFactory.GetInstance(driverType);
     ConnectionInfo = connectionInfo;
     Connection     = Driver.BuildConnection(ConnectionInfo);
     OpenConnection();
 }
Ejemplo n.º 4
0
        public void Run(IDbConnectionInfo connectionInfo)
        {
            if (ScriptType != ScriptType.Runnable) throw new ArgumentException(string.Format("ScriptType is not runnable : {0}", ScriptType), "ScriptType");

            try
            {
                var rType = new ReflectionType(ScriptValue);
                var runMethod = rType.CreateType().GetMethod("Run");
                if (runMethod == null)
                    throw new ArgumentException(string.Format("Could not find a method named 'Run' for ScriptValue : {0}", ScriptValue), "ScriptValue");
                if (runMethod.ReturnType != typeof(void))
                    throw new ArgumentException(string.Format("Run method does not have return type 'void' for ScriptValue : {0}", ScriptValue), "ScriptValue");
                var parameters = runMethod.GetParameters();
                if (parameters.Length != 1 || parameters[0].ParameterType != typeof(IDbConnectionInfo))
                    throw new ArgumentException(
                        string.Format("Run method does not have a single parameter of type 'IDbConnectionInfo' for ScriptValue : {0}", ScriptValue), "ScriptValue");

                runMethod.Invoke(rType.CreateObject(), BindingFlags.InvokeMethod, null, new object[] {connectionInfo}, null);
            }
            catch (Exception e)
            {
                if (e.GetType() == typeof(ArgumentException)) throw;
                throw new ArgumentException(string.Format("ScriptValue could not be run : {0} : {1} : {2}", ScriptValue, e.GetType(), e.Message), "ScriptValue", e);
            }
        }
Ejemplo n.º 5
0
    /// <summary>
    /// Creates if the database does not exist, otherwise opens
    /// </summary>
    /// <param name="connectionInfo"></param>
    /// <returns></returns>
    public static IMockDatabase Create(IDbConnectionInfo connectionInfo)
    {
      connectionInfo = VerifyConnectionInfo(connectionInfo);

      var databaseType = ((IMockDbConnectionInfo)connectionInfo).DatabaseType;

      if (!mockDatabases.ContainsKey(databaseType))
      {
        // Check that the type is OK
        var interfaces = databaseType.FindInterfaces((type, criteria) => type == typeof(IMockDatabase), null);
        if (interfaces.Length == 0) throw new ArgumentException(string.Format("Mock database type does not implement IMockDatabase : {0}", databaseType), "connectionInfo.DatabaseType");

        if (databaseType.GetConstructor(Type.EmptyTypes) == null) throw new ArgumentException(string.Format("Mock database type does not have a parameterless constructor : {0}", databaseType), "connectionInfo.DatabaseType");

        mockDatabases.Add(databaseType, new Dictionary<string, IMockDatabase>());
      }

      if (!mockDatabases[databaseType].ContainsKey(connectionInfo.DatabaseName))
      {
        var ctor = databaseType.GetConstructor(Type.EmptyTypes);
        mockDatabases[databaseType].Add(connectionInfo.DatabaseName, (IMockDatabase)ctor.Invoke(null));
      }

      return mockDatabases[databaseType][connectionInfo.DatabaseName];
    }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates if the database does not exist, otherwise opens
        /// </summary>
        /// <param name="connectionInfo"></param>
        /// <returns></returns>
        public static IMockDatabase Create(IDbConnectionInfo connectionInfo)
        {
            connectionInfo = VerifyConnectionInfo(connectionInfo);

            var databaseType = ((IMockDbConnectionInfo)connectionInfo).DatabaseType;

            if (!mockDatabases.ContainsKey(databaseType))
            {
                // Check that the type is OK
                var interfaces = databaseType.FindInterfaces((type, criteria) => type == typeof(IMockDatabase), null);
                if (interfaces.Length == 0)
                {
                    throw new ArgumentException(string.Format("Mock database type does not implement IMockDatabase : {0}", databaseType), "connectionInfo.DatabaseType");
                }

                if (databaseType.GetConstructor(Type.EmptyTypes) == null)
                {
                    throw new ArgumentException(string.Format("Mock database type does not have a parameterless constructor : {0}", databaseType), "connectionInfo.DatabaseType");
                }

                mockDatabases.Add(databaseType, new Dictionary <string, IMockDatabase>());
            }

            if (!mockDatabases[databaseType].ContainsKey(connectionInfo.DatabaseName))
            {
                var ctor = databaseType.GetConstructor(Type.EmptyTypes);
                mockDatabases[databaseType].Add(connectionInfo.DatabaseName, (IMockDatabase)ctor.Invoke(null));
            }

            return(mockDatabases[databaseType][connectionInfo.DatabaseName]);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 快速创建命令
        /// </summary>
        /// <param name="dbConnectionInfo">连接对象</param>
        /// <returns>命令</returns>
        public static IDbCommand CreateCommand(IDbConnectionInfo dbConnectionInfo)
        {
            var config = DatabaseConfigManager.GetConfig(dbConnectionInfo.DBID);
            var cmd    = dbConnectionInfo.DbConnection.CreateCommand();

            SetCommandPara(cmd, config);
            return(cmd);
        }
Ejemplo n.º 8
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="conInfo">连接信息对象</param>
 /// <param name="customRegisteEntityTypeFunc">自定义注册EF实体类型回调,已自定义注册实体返回true;否则返回false</param>
 public EFDbContext(IDbConnectionInfo conInfo, Func <DatabaseConfig, DbModelBuilder, bool> customRegisteEntityTypeFunc)
     : base(conInfo.DbConnection, false)
 {
     this._conInfo = conInfo;
     this._customRegisteEntityTypeFunc = customRegisteEntityTypeFunc;
     Database.SetInitializer <EFDbContext>(null);
     this.RegisteLogOutput();
 }
Ejemplo n.º 9
0
    public void Run(IDbConnectionInfo connectionInfo)
    {
      var db = MockDatabaseProvider.Open(connectionInfo) as TestMockDatabase;
      if (db == null) throw new ArgumentException("Could not open mock database from supplied connectionInfo", "connectionInfo");

      // Schema already defined in TestMockDatabase

      // TODO: Is there a point to this for mock databases?
    }
Ejemplo n.º 10
0
    /// <summary>
    /// Opens an existing database
    /// </summary>
    /// <param name="connectionInfo"></param>
    /// <returns></returns>
    public static IMockDatabase Open(IDbConnectionInfo connectionInfo)
    {
      connectionInfo = VerifyConnectionInfo(connectionInfo);

      var databaseType = ((IMockDbConnectionInfo) connectionInfo).DatabaseType;

      if (!mockDatabases.ContainsKey(databaseType)) throw new ArgumentException("Database does not exist", "connectionInfo");
      if (!mockDatabases[databaseType].ContainsKey(connectionInfo.DatabaseName)) throw new ArgumentException("Database does not exist", "connectionInfo");

      return mockDatabases[databaseType][connectionInfo.DatabaseName];
    }
Ejemplo n.º 11
0
        private static IDbConnectionInfo VerifyConnectionInfo(IDbConnectionInfo connectionInfo)
        {
            if (connectionInfo == null)
            {
                throw new ArgumentException("connectionInfo not provided", "connectionInfo");
            }

            return(new MockDbConnectionInfo {
                ConnectionString = connectionInfo.ConnectionString
            });
        }
Ejemplo n.º 12
0
        public void Run(IDbConnectionInfo connectionInfo)
        {
            var db = MockDatabaseProvider.Open(connectionInfo) as TestMockDatabase;

            if (db == null)
            {
                throw new ArgumentException("Could not open mock database from supplied connectionInfo", "connectionInfo");
            }

            db.Table.Add(new TestMockDatabase.Row {
                Id = 3, RowName = "Three"
            });
        }
Ejemplo n.º 13
0
        public void Run(IDbConnectionInfo connectionInfo)
        {
            var db = MockDatabaseProvider.Open(connectionInfo) as TestMockDatabase;

            if (db == null)
            {
                throw new ArgumentException("Could not open mock database from supplied connectionInfo", "connectionInfo");
            }

            // Schema already defined in TestMockDatabase

            // TODO: Is there a point to this for mock databases?
        }
Ejemplo n.º 14
0
    /// <summary>
    /// Destroys the database if it exists
    /// </summary>
    /// <param name="connectionInfo"></param>
    /// <returns></returns>
    public static void Destroy(IDbConnectionInfo connectionInfo)
    {
      connectionInfo = VerifyConnectionInfo(connectionInfo);

      var databaseType = ((IMockDbConnectionInfo)connectionInfo).DatabaseType;

      if(!mockDatabases.ContainsKey(databaseType)) return;

      mockDatabases[databaseType].Remove(connectionInfo.DatabaseName);

      if(mockDatabases[databaseType].Count == 0)
      {
        mockDatabases.Remove(databaseType);
      }
    }
Ejemplo n.º 15
0
        /// <summary>
        /// Opens an existing database
        /// </summary>
        /// <param name="connectionInfo"></param>
        /// <returns></returns>
        public static IMockDatabase Open(IDbConnectionInfo connectionInfo)
        {
            connectionInfo = VerifyConnectionInfo(connectionInfo);

            var databaseType = ((IMockDbConnectionInfo)connectionInfo).DatabaseType;

            if (!mockDatabases.ContainsKey(databaseType))
            {
                throw new ArgumentException("Database does not exist", "connectionInfo");
            }
            if (!mockDatabases[databaseType].ContainsKey(connectionInfo.DatabaseName))
            {
                throw new ArgumentException("Database does not exist", "connectionInfo");
            }

            return(mockDatabases[databaseType][connectionInfo.DatabaseName]);
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Destroys the database if it exists
        /// </summary>
        /// <param name="connectionInfo"></param>
        /// <returns></returns>
        public static void Destroy(IDbConnectionInfo connectionInfo)
        {
            connectionInfo = VerifyConnectionInfo(connectionInfo);

            var databaseType = ((IMockDbConnectionInfo)connectionInfo).DatabaseType;

            if (!mockDatabases.ContainsKey(databaseType))
            {
                return;
            }

            mockDatabases[databaseType].Remove(connectionInfo.DatabaseName);

            if (mockDatabases[databaseType].Count == 0)
            {
                mockDatabases.Remove(databaseType);
            }
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Builds a Postgres connection object for the given remote Postgres server.
        /// </summary>
        /// <param name="connectionInfo">The database connection information.</param>
        /// <returns>Connection object for the remote Postgres server.</returns>
        public IDbConnection BuildConnection(IDbConnectionInfo connectionInfo)
        {
            if (connectionInfo.Port == null)
            {
                throw new ArgumentException("Port cannot be null!");
            }

            var connectionString =
                new NpgsqlConnectionStringBuilder()
                {
                    Host = connectionInfo.Server,
                    Port = connectionInfo.Port.Value,
                    UserName = connectionInfo.Username,
                    Password = connectionInfo.Password,
                    Database = connectionInfo.DatabaseName
                };

            return new NpgsqlConnection(connectionString);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Builds a Postgres connection object for the given remote Postgres server.
        /// </summary>
        /// <param name="connectionInfo">The database connection information.</param>
        /// <returns>Connection object for the remote Postgres server.</returns>
        public IDbConnection BuildConnection(IDbConnectionInfo connectionInfo)
        {
            if (connectionInfo.Port == null)
            {
                throw new ArgumentException("Port cannot be null!");
            }

            var connectionString =
                new NpgsqlConnectionStringBuilder()
            {
                Host     = connectionInfo.Server,
                Port     = connectionInfo.Port.Value,
                UserName = connectionInfo.Username,
                Password = connectionInfo.Password,
                Database = connectionInfo.DatabaseName
            };

            return(new NpgsqlConnection(connectionString));
        }
Ejemplo n.º 19
0
        public void Run(IDbConnectionInfo connectionInfo)
        {
            if (ScriptType != ScriptType.Runnable)
            {
                throw new ArgumentException(string.Format("ScriptType is not runnable : {0}", ScriptType), "ScriptType");
            }

            try
            {
                var rType     = new ReflectionType(ScriptValue);
                var runMethod = rType.CreateType().GetMethod("Run");
                if (runMethod == null)
                {
                    throw new ArgumentException(string.Format("Could not find a method named 'Run' for ScriptValue : {0}", ScriptValue), "ScriptValue");
                }
                if (runMethod.ReturnType != typeof(void))
                {
                    throw new ArgumentException(string.Format("Run method does not have return type 'void' for ScriptValue : {0}", ScriptValue), "ScriptValue");
                }
                var parameters = runMethod.GetParameters();
                if (parameters.Length != 1 || parameters[0].ParameterType != typeof(IDbConnectionInfo))
                {
                    throw new ArgumentException(
                              string.Format("Run method does not have a single parameter of type 'IDbConnectionInfo' for ScriptValue : {0}", ScriptValue), "ScriptValue");
                }

                runMethod.Invoke(rType.CreateObject(), BindingFlags.InvokeMethod, null, new object[] { connectionInfo }, null);
            }
            catch (Exception e)
            {
                if (e.GetType() == typeof(ArgumentException))
                {
                    throw;
                }
                throw new ArgumentException(string.Format("ScriptValue could not be run : {0} : {1} : {2}", ScriptValue, e.GetType(), e.Message), "ScriptValue", e);
            }
        }
Ejemplo n.º 20
0
 public string Run(IDbConnectionInfo connectionInfo)
 {
     return(null);
 }
Ejemplo n.º 21
0
 public void InvalidMethodName(IDbConnectionInfo connectionInfo)
 {
 }
Ejemplo n.º 22
0
 public DbHelperBase(IDbConnectionInfo conn)
     : this(conn.ToConnectionString())
 {
     _dbConnectionInfo = conn;
 }
Ejemplo n.º 23
0
 public DataTableDbWriter(DbDriverType driverType, IDbConnectionInfo connectionInfo, DbTableInitializationOptions tableInitializationOptions = default(DbTableInitializationOptions))
 {
     Adapter = new DbAdapter(driverType, connectionInfo);
     this.tableInitializationOptions = tableInitializationOptions;
 }
Ejemplo n.º 24
0
    private static IDbConnectionInfo VerifyConnectionInfo(IDbConnectionInfo connectionInfo)
    {
      if (connectionInfo == null) throw new ArgumentException("connectionInfo not provided", "connectionInfo");

      return new MockDbConnectionInfo {ConnectionString = connectionInfo.ConnectionString};
    }
Ejemplo n.º 25
0
        public static string CreateDbLinkConnection(IDbConnectionInfo connectionInfo)
        {
            var builder = new NpgsqlConnectionStringBuilder(connectionInfo.ConnectionString);
            var dbLink = new StringBuilder();
            
            if (builder.ContainsKey(PgDbConnectionInfo.ServerAddressKey))
            {
                if (dbLink.Length > 0) dbLink.Append(" ");
                dbLink.Append("host=").Append(builder[PgDbConnectionInfo.ServerAddressKey]);
            }

            if (builder.ContainsKey(PgDbConnectionInfo.ServerPortKey))
            {
                if (dbLink.Length > 0) dbLink.Append(" ");
                dbLink.Append("port=").Append(builder[PgDbConnectionInfo.ServerPortKey]);
            }

            if (builder.ContainsKey(PgDbConnectionInfo.DatabaseNameKey))
            {
                if (dbLink.Length > 0) dbLink.Append(" ");
                dbLink.Append("dbname=").Append(builder[PgDbConnectionInfo.DatabaseNameKey]);
            }

            if (builder.ContainsKey(PgDbConnectionInfo.UserNameKey))
            {
                if (dbLink.Length > 0) dbLink.Append(" ");
                dbLink.Append("user="******" ");
                dbLink.Append("user="******" ");
                dbLink.Append("password=").Append(builder[PgDbConnectionInfo.PasswordKey]);
            }
            return dbLink.ToString();
        }
Ejemplo n.º 26
0
 public void Run(IDbConnectionInfo connectionInfo)
 {
     throw new Exception("Method was called");
 }
Ejemplo n.º 27
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="conInfo">连接信息对象</param>
 public TestContext(IDbConnectionInfo conInfo)
     : base(conInfo, null)
 {
 }
Ejemplo n.º 28
0
 public void InvalidMethodName(IDbConnectionInfo connectionInfo)
 {
 }
Ejemplo n.º 29
0
 public string Run(IDbConnectionInfo connectionInfo)
 {
     return null;
 }
 /// <summary>
 /// Create using existing connection information
 /// </summary>
 /// <param name="connectionInfo">Connection information</param>
 public MicrOrmConnectionProvider(IDbConnectionInfo connectionInfo)
 {
     ConnectionInfo = connectionInfo;
 }
Ejemplo n.º 31
0
 public void Run(IDbConnectionInfo connectionInfo)
 {
     throw new Exception("Method was called");
 }
Ejemplo n.º 32
0
 public DataTableDbWriter(DbDriverType driverType, IDbConnectionInfo connectionInfo, DbTableInitializationOptions tableInitializationOptions = default(DbTableInitializationOptions))
 {
     Adapter = new DbAdapter(driverType, connectionInfo);
     this.tableInitializationOptions = tableInitializationOptions;
 }
 /// <summary>
 /// Create using existing connection information
 /// </summary>
 /// <param name="connectionInfo">Connection information</param>
 public MicrOrmConnectionProvider(IDbConnectionInfo connectionInfo)
 {
     ConnectionInfo = connectionInfo;
 }