Exemplo n.º 1
0
        public IDatabaseFormFiller GetFormFillerFor(IDatabaseConnector connector)
        {
            if (connector is ISQLServer2005DatabaseConnector)
                return new SQLServer2005DatabaseFormFiller(connector as ISQLServer2005DatabaseConnector);

            if (connector is ISQLCEDatabaseConnector)
                return new SQLServerCEDatabaseFormFiller(connector as ISQLCEDatabaseConnector);

            if (connector is ISQLServerExpressDatabaseConnector)
                return new SQLServerExpressDatabaseFormFiller(connector as ISQLServerExpressDatabaseConnector);

            if (connector is IOracleDatabaseConnector)
                return new OracleDatabaseFormFiller(connector as IOracleDatabaseConnector);

            if (connector is IPostgreSQLDatabaseConnector)
                return new PostgreSQLDatabaseFormFiller(connector as IPostgreSQLDatabaseConnector);

            if (connector is IMySQLDatabaseConnector)
                return new MySQLDatabaseFormFiller(connector as IMySQLDatabaseConnector);

            if (connector is IFirebirdDatabaseConnector)
                return new FirebirdDatabaseFormFiller(connector as IFirebirdDatabaseConnector);

            if (connector is ISQLiteDatabaseConnector)
                return new SQLiteDatabaseFormFiller(connector as ISQLiteDatabaseConnector);

            throw new ArgumentException("DatabaseFormFillerFactory does not support " + connector.GetType() + " connectors yet.");
        }
Exemplo n.º 2
0
        /// <summary>
        /// Select first row from table.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connector"></param>
        /// <param name="transaction">Transaction for current execution.</param>
        /// <param name="buffered">Whether the data should be cached in memory.</param>
        /// <returns>Object of given class</returns>
        public static T QueryFirst <T>(this IDatabaseConnector connector, DbTransaction?transaction = null, bool buffered = true) where T : class, new()
        {
            var con = connector as DatabaseConnectorBase;

            if (con == null)
            {
                throw new InvalidCastException($"{connector.GetType().FullName} cannot be use with this extension (expected to get instance of {typeof(DatabaseConnectorBase).FullName}");
            }
            var query  = con.SelectQueryGenerate <T>(top: 1);
            T   result = con.ExecuteReader <T>(query, transaction: transaction, buffered: buffered).FirstOrDefault();

            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Select first row from table by using matched predicate.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connector"></param>
        /// <param name="predicate">Predicate of data in LINQ manner</param>
        /// <param name="transaction">Transaction for current execution.</param>
        /// <param name="buffered">Whether the data should be cached in memory.</param>
        /// <returns>Object of given class</returns>
        public static async Task <T> QueryFirstAsync <T>(this IDatabaseConnector connector, Expression <Func <T, bool> > predicate, DbTransaction?transaction = null, bool buffered = true) where T : class, new()
        {
            var con = connector as DatabaseConnectorBase;

            if (con == null)
            {
                throw new InvalidCastException($"{connector.GetType().FullName} cannot be use with this extension (expected to get instance of {typeof(DatabaseConnectorBase).FullName}");
            }
            var query  = con.SelectQueryGenerate <T>(predicate, 1);
            T   result = (await con.ExecuteReaderAsync <T>(query.query, query.parameters, transaction, buffered: buffered).ConfigureAwait(false)).FirstOrDefault();

            return(result);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Select data from table by using primary key
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connector"></param>
        /// <param name="primaryKey">Specified primary key.</param>
        /// <param name="transaction">Transaction for current execution.</param>
        /// <returns></returns>
        public static async Task <int> DeleteAsync <T>(this IDatabaseConnector connector, object primaryKey, DbTransaction?transaction = null) where T : class, new()
        {
            var con = connector as DatabaseConnectorBase;

            if (con == null)
            {
                throw new InvalidCastException($"{connector.GetType().FullName} cannot be use with this extension (expected to get instance of {typeof(DatabaseConnectorBase).FullName}");
            }
            var preparer   = con.DeleteQueryGenerate <T>(primaryKey);
            var query      = preparer.query;
            var parameters = preparer.parameters;

            return(await con.ExecuteNonQueryAsync(query, parameters, transaction : transaction).ConfigureAwait(false));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Delete data from table by using matched predicate
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connector"></param>
        /// <param name="predicate">Predicate of data in LINQ manner</param>
        /// <param name="transaction">Transaction for current execution.</param>
        /// <returns></returns>
        public static int Delete <T>(this IDatabaseConnector connector, Expression <Func <T, bool> > predicate, DbTransaction?transaction = null) where T : class, new()
        {
            var con = connector as DatabaseConnectorBase;

            if (con == null)
            {
                throw new InvalidCastException($"{connector.GetType().FullName} cannot be use with this extension (expected to get instance of {typeof(DatabaseConnectorBase).FullName}");
            }
            var preparer   = con.DeleteQueryGenerate <T>(predicate);
            var query      = preparer.query;
            var parameters = preparer.parameters;

            return(con.ExecuteNonQuery(query, parameters, transaction: transaction));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Select all rows from table (table name is a class name or specific [Table] attribute, an attribute has higher priority).
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connector"></param>
        /// <param name="top">Specified TOP(n) rows.</param>
        /// <param name="transaction">Transaction for current execution.</param>
        /// <param name="buffered">Whether the data should be cached in memory.</param>
        /// <returns>IEnumerable of object</returns>
        public static async Task <IEnumerable <T> > QueryAsync <T>(this IDatabaseConnector connector, int?top = null, DbTransaction?transaction = null, bool buffered = true)
            where T : class, new()
        {
            var con = connector as DatabaseConnectorBase;

            if (con == null)
            {
                throw new InvalidCastException($"{connector.GetType().FullName} cannot be use with this extension (expected to get instance of {typeof(DatabaseConnectorBase).FullName}");
            }
            var query  = con.SelectQueryGenerate <T>(top);
            var result = await con.ExecuteReaderAsync <T>(query, transaction : transaction, buffered : buffered).ConfigureAwait(false);

            return(result);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Returns rows count from specified table.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static int Count <T>(this IDatabaseConnector connector) where T : class
        {
            var con = connector as DatabaseConnectorBase;

            if (con == null)
            {
                throw new InvalidCastException($"{connector.GetType().FullName} cannot be use with this extension (expected to get instance of {typeof(DatabaseConnectorBase).FullName}");
            }
            var tableName     = AttributeExtension.TableNameAttributeValidate(typeof(T));
            var query         = $"SELECT COUNT(*) FROM {tableName}";
            var count         = con.ExecuteScalar(query);
            var countAsString = count.ToString();

            return(int.Parse(countAsString));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Select data from table by using matched predicate
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connector"></param>
        /// <param name="predicate">Predicate of data in LINQ manner</param>
        /// <param name="top">Specified TOP(n) rows.</param>
        /// <param name="transaction">Transaction for current execution.</param>
        /// <param name="buffered">Whether the data should be cached in memory.</param>
        /// <returns></returns>
        public static IEnumerable <T> Query <T>(this IDatabaseConnector connector, Expression <Func <T, bool> > predicate, int?top = null, DbTransaction?transaction = null, bool buffered = true) where T : class, new()
        {
            var con = connector as DatabaseConnectorBase;

            if (con == null)
            {
                throw new InvalidCastException($"{connector.GetType().FullName} cannot be use with this extension (expected to get instance of {typeof(DatabaseConnectorBase).FullName}");
            }
            var preparer   = con.SelectQueryGenerate <T>(predicate, top);
            var query      = preparer.query;
            var parameters = preparer.parameters;
            var result     = con.ExecuteReader <T>(query, parameters, transaction: transaction, buffered: buffered);

            return(result);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Insert rows into table (table name is a class name or specific [Table] attribute, an attribute has higher priority).
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connector"></param>
        /// <param name="obj">Object to insert.</param>
        /// <param name="transaction">Transaction for current execution.</param>
        /// <returns>Affected row after an insert.</returns>
        public static async Task <int> InsertManyAsync <T>(this IDatabaseConnector connector, IEnumerable <T> obj, DbTransaction?transaction = null) where T : class, new()
        {
            var con = connector as DatabaseConnectorBase;

            if (con == null)
            {
                throw new InvalidCastException($"{connector.GetType().FullName} cannot be use with this extension (expected to get instance of {typeof(DatabaseConnectorBase).FullName}");
            }
            var preparer   = con.InsertQueryGenerate <T>(obj);
            var query      = preparer.query;
            var parameters = preparer.parameters;
            var result     = await con.ExecuteNonQueryAsync(query, parameters, transaction : transaction).ConfigureAwait(false);

            return(result);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Returns rows count on specific condition from specified table.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static int Count <T>(this IDatabaseConnector connector, Expression <Func <T, bool> > predicate) where T : class
        {
            var con = connector as DatabaseConnectorBase;

            if (con == null)
            {
                throw new InvalidCastException($"{connector.GetType().FullName} cannot be use with this extension (expected to get instance of {typeof(DatabaseConnectorBase).FullName}");
            }
            var tableName       = AttributeExtension.TableNameAttributeValidate(typeof(T));
            var exprTranslator  = new ExpressionTranslator <T>(con.CompatibleFunctionName);
            var translateResult = exprTranslator.Translate(predicate);
            var query           = $"SELECT COUNT(*) FROM {tableName} WHERE {translateResult.Expression}";
            var count           = con.ExecuteScalar(query, translateResult.Parameters);
            var countAsString   = count.ToString();

            return(int.Parse(countAsString));
        }
Exemplo n.º 11
0
        /// <summary>
        /// Insert row into table (table name is a class name or specific [Table] attribute, an attribute has higher priority).
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connector"></param>
        /// <param name="obj">Object to insert.</param>
        /// <param name="transaction">Transaction for current execution.</param>
        /// <returns>Affected row after an insert.</returns>
        public static int Insert <T>(this IDatabaseConnector connector, T obj, DbTransaction?transaction = null)
            where T : class, new()
        {
            var con = connector as DatabaseConnectorBase;

            if (con == null)
            {
                throw new InvalidCastException($"{connector.GetType().FullName} cannot be use with this extension (expected to get instance of {typeof(DatabaseConnectorBase).FullName}");
            }
            if (obj == null)
            {
                return(-1);
            }
            var preparer   = con.InsertQueryGenerate <T>(obj);
            var query      = preparer.query;
            var parameters = preparer.parameters;
            var result     = con.ExecuteNonQuery(query, parameters, transaction: transaction);

            return(result);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Gets called when loading an existing VS project.
        /// </summary>
        /// <param name="connector"></param>
        /// <returns></returns>
        public static IDatabaseLoader GetDatabaseLoader(IDatabaseConnector connector)
        {
            if (connector is ISQLCEDatabaseConnector)
                return new SQLCEDatabaseLoader(connector as ISQLCEDatabaseConnector);

            if (connector is ISQLServer2005DatabaseConnector)
                return new SQLServer2005DatabaseLoader(connector as ISQLServer2005DatabaseConnector);

            if (connector is IMySQLDatabaseConnector)
                return new MySQLDatabaseLoader(connector as IMySQLDatabaseConnector);

            if (connector is IOracleDatabaseConnector)
                return new OracleDatabaseLoader(connector as IOracleDatabaseConnector);

            if (connector is IPostgreSQLDatabaseConnector)
                return new PostgreSQLDatabaseLoader(connector as IPostgreSQLDatabaseConnector);

            if (connector is IFirebirdDatabaseConnector)
                return new FirebirdDatabaseLoader(connector as IFirebirdDatabaseConnector);

            throw new Exception("DatabaseLoaderFacade does not know how to deal with a connector of type " + connector.GetType());
        }
Exemplo n.º 13
0
        public IDatabaseFormFiller GetFormFillerFor(IDatabaseConnector connector)
        {
            if (connector is ISQLServer2005DatabaseConnector)
            {
                return(new SQLServer2005DatabaseFormFiller(connector as ISQLServer2005DatabaseConnector));
            }

            if (connector is ISQLCEDatabaseConnector)
            {
                return(new SQLServerCEDatabaseFormFiller(connector as ISQLCEDatabaseConnector));
            }

            if (connector is ISQLServerExpressDatabaseConnector)
            {
                return(new SQLServerExpressDatabaseFormFiller(connector as ISQLServerExpressDatabaseConnector));
            }

            if (connector is IOracleDatabaseConnector)
            {
                return(new OracleDatabaseFormFiller(connector as IOracleDatabaseConnector));
            }

            if (connector is IPostgreSQLDatabaseConnector)
            {
                return(new PostgreSQLDatabaseFormFiller(connector as IPostgreSQLDatabaseConnector));
            }

            if (connector is IMySQLDatabaseConnector)
            {
                return(new MySQLDatabaseFormFiller(connector as IMySQLDatabaseConnector));
            }

            if (connector is IFirebirdDatabaseConnector)
            {
                return(new FirebirdDatabaseFormFiller(connector as IFirebirdDatabaseConnector));
            }

            if (connector is ISQLiteDatabaseConnector)
            {
                return(new SQLiteDatabaseFormFiller(connector as ISQLiteDatabaseConnector));
            }

            throw new ArgumentException("DatabaseFormFillerFactory does not support " + connector.GetType() + " connectors yet.");
        }
 private void SerialiseConnectionInformationInternal(IDatabaseConnector connector, XmlWriter writer)
 {
     if (connector is ISQLServer2005DatabaseConnector)
     {
         new SQLServer2005ConnectorSerialiser().Serialise(connector as ISQLServer2005DatabaseConnector, writer);
     }
     else if (connector is ISQLCEDatabaseConnector)
     {
         new SQLCEConnectorSerialiser().Serialise(connector as ISQLCEDatabaseConnector, writer);
     }
     else if (connector is IMySQLDatabaseConnector)
     {
         new MySQLConnectorSerialiser().Serialise(connector as IMySQLDatabaseConnector, writer);
     }
     else if (connector is IOracleDatabaseConnector)
     {
         new OracleConnectorSerialiser().Serialise(connector as IOracleDatabaseConnector, writer);
     }
     else if (connector is IPostgreSQLDatabaseConnector)
     {
         new PostgreSQLConnectorSerialiser().Serialise(connector as IPostgreSQLDatabaseConnector, writer);
     }
     else if (connector is ISQLServerExpressDatabaseConnector)
     {
         new SQLServerExpressConnectorSerialiser().Serialise(connector as ISQLServerExpressDatabaseConnector, writer);
     }
     else if (connector is IFirebirdDatabaseConnector)
     {
         new FirebirdConnectorSerialiser().Serialise(connector as IFirebirdDatabaseConnector, writer);
     }
     else if (connector is ISQLiteDatabaseConnector)
     {
         new SQLiteConnectorSerialiser().Serialise(connector as ISQLiteDatabaseConnector, writer);
     }
     else
     {
         throw new NotImplementedException("Not coded for yet in SerialiseConnectionInformationInternal(): " + connector.GetType().Name);
     }
 }
 private void SerialiseConnectionInformationInternal(IDatabaseConnector connector, XmlWriter writer)
 {
     if (connector is ISQLServer2005DatabaseConnector)
     {
         new SQLServer2005ConnectorSerialiser().Serialise(connector as ISQLServer2005DatabaseConnector, writer);
     }
     else if (connector is ISQLCEDatabaseConnector)
     {
         new SQLCEConnectorSerialiser().Serialise(connector as ISQLCEDatabaseConnector, writer);
     }
     else if (connector is IMySQLDatabaseConnector)
     {
         new MySQLConnectorSerialiser().Serialise(connector as IMySQLDatabaseConnector, writer);
     }
     else if (connector is IOracleDatabaseConnector)
     {
         new OracleConnectorSerialiser().Serialise(connector as IOracleDatabaseConnector, writer);
     }
     else if (connector is IPostgreSQLDatabaseConnector)
     {
         new PostgreSQLConnectorSerialiser().Serialise(connector as IPostgreSQLDatabaseConnector, writer);
     }
     else if (connector is ISQLServerExpressDatabaseConnector)
     {
         new SQLServerExpressConnectorSerialiser().Serialise(connector as ISQLServerExpressDatabaseConnector, writer);
     }
     else if (connector is IFirebirdDatabaseConnector)
     {
         new FirebirdConnectorSerialiser().Serialise(connector as IFirebirdDatabaseConnector, writer);
     }
     else if (connector is ISQLiteDatabaseConnector)
     {
         new SQLiteConnectorSerialiser().Serialise(connector as ISQLiteDatabaseConnector, writer);
     }
     else
     {
         throw new NotImplementedException("Not coded for yet in SerialiseConnectionInformationInternal(): " + connector.GetType().Name);
     }
 }
Exemplo n.º 16
0
        /// <summary>
        /// Gets called when loading an existing VS project.
        /// </summary>
        /// <param name="connector"></param>
        /// <returns></returns>
        public static IDatabaseLoader GetDatabaseLoader(IDatabaseConnector connector)
        {
            if (connector is ISQLCEDatabaseConnector)
            {
                return(new SQLCEDatabaseLoader(connector as ISQLCEDatabaseConnector));
            }

            if (connector is ISQLServer2005DatabaseConnector)
            {
                return(new SQLServer2005DatabaseLoader(connector as ISQLServer2005DatabaseConnector));
            }

            if (connector is IMySQLDatabaseConnector)
            {
                return(new MySQLDatabaseLoader(connector as IMySQLDatabaseConnector));
            }

            if (connector is IOracleDatabaseConnector)
            {
                return(new OracleDatabaseLoader(connector as IOracleDatabaseConnector));
            }

            if (connector is IPostgreSQLDatabaseConnector)
            {
                return(new PostgreSQLDatabaseLoader(connector as IPostgreSQLDatabaseConnector));
            }

            if (connector is IFirebirdDatabaseConnector)
            {
                return(new FirebirdDatabaseLoader(connector as IFirebirdDatabaseConnector));
            }

            throw new Exception("DatabaseLoaderFacade does not know how to deal with a connector of type " + connector.GetType());
        }