Пример #1
0
        /// <summary>
        /// Deletes the rows that match the given condition.
        /// </summary>
        /// <typeparam name="T">The table type.</typeparam>
        /// <param name="connection">The connection to query on.</param>
        /// <param name="whereExpr">The where condition to use for this query.</param>
        /// <param name="transaction">The transaction to use for this query.</param>
        /// <param name="commandTimeout">Number of seconds before command execution timeout.</param>
        /// <returns>The number of deleted rows.</returns>
        public static int DeleteList <T>(this IDbConnection connection, Expression <Func <T, bool> > whereExpr, IDbTransaction transaction = null, int commandTimeout = 30)
            where T : class
        {
            ISqlQueries <T>        queries = ExtraCrud.Queries <T>();
            WhereConditionData <T> data    = queries.Compile(whereExpr);
            int count = queries.DeleteList(connection, data.WhereCondition, data.Param, transaction, commandTimeout);

            return(count);
        }
Пример #2
0
        /// <summary>
        /// Selects the rows that match the given condition.
        /// </summary>
        /// <typeparam name="T">The table type.</typeparam>
        /// <param name="connection">The connection to query on.</param>
        /// <param name="columnFilter">The type whose properties will filter the result.</param>
        /// <param name="whereExpr">The where condition to use for this query.</param>
        /// <param name="transaction">The transaction to use for this query.</param>
        /// <param name="buffered">Whether to buffer the results in memory.</param>
        /// <param name="commandTimeout">Number of seconds before command execution timeout.</param>
        /// <returns>The rows that match the given condition.</returns>
        public static IEnumerable <T> GetDistinct <T>(this IDbConnection connection, Type columnFilter, Expression <Func <T, bool> > whereExpr, IDbTransaction transaction = null, bool buffered = true, int commandTimeout = 30)
            where T : class
        {
            ISqlQueries <T>        queries = ExtraCrud.Queries <T>();
            WhereConditionData <T> data    = queries.Compile(whereExpr);
            IEnumerable <T>        list    = queries.GetDistinct(connection, columnFilter, data.WhereCondition, data.Param, transaction, buffered, commandTimeout);

            return(list);
        }
Пример #3
0
        internal CacheAutoStorage()
        {
            SqlBuilder <T> builder = ExtraCrud.Builder <T>();

            if (builder.Info.KeyColumns.Count == 1)
            {
                ObjectFromKey = builder.ObjectFromKey;
            }
            Cache = new Dictionary <T, R>(builder.EqualityComparer);
        }
 internal DbCacheTable(string connectionString)
 {
     ConnectionString = connectionString;
     Builder          = ExtraCrud.Builder <T>();
     if (Builder.Info.KeyColumns.Count == 0)
     {
         throw new InvalidOperationException(typeof(T).FullName + " is not usable without a valid key.");
     }
     DAO            = new DataAccessObject <T>(connectionString);
     AAO            = new AutoAccessObject <T>(connectionString);
     Access         = AAO;
     AutoCache      = new CacheAutoStorage <T, R>();
     Items          = AutoCache;
     CreateFromKey  = Builder.ObjectFromKey;
     AutoKeyColumn  = Builder.Info.AutoKeyColumn;
     AutoSyncInsert = Builder.Queries.InsertAutoSync != null;
     AutoSyncUpdate = Builder.Queries.UpdateAutoSync != null;
 }
Пример #5
0
        /// <summary>
        /// Selects the rows with the given keys.
        /// </summary>
        /// <typeparam name="T">The table type.</typeparam>
        /// <typeparam name="KeyType">The key type.</typeparam>
        /// <param name="connection">The connection to query on.</param>
        /// <param name="whereExpr">The where condition to use for this query.</param>
        /// <param name="transaction">The transaction to use for this query.</param>
        /// <param name="buffered">Whether to buffer the results in memory.</param>
        /// <param name="commandTimeout">Number of seconds before command execution timeout.</param>
        /// <returns>The keys that match the given condition.</returns>
        public static IEnumerable <KeyType> GetKeys <T, KeyType>(this IDbConnection connection, Expression <Func <T, bool> > whereExpr, IDbTransaction transaction = null, bool buffered = true, int commandTimeout = 30)
            where T : class
        {
            ISqlQueries <T>        queries = ExtraCrud.Queries <T>();
            WhereConditionData <T> data    = queries.Compile(whereExpr);
            IEnumerable <object>   keys    = queries.GetKeysKeys(connection, data.WhereCondition, data.Param, transaction, buffered, commandTimeout);

            if (typeof(KeyType) == typeof(long))
            {
                if (keys.Any())
                {
                    Type type = keys.First().GetType();
                    if (type == typeof(int))
                    {
                        keys = keys.Select(k => (object)(long)(int)k);
                    }
                }
            }
            IEnumerable <KeyType> castedKeys = keys.Select(k => (KeyType)k);

            return(castedKeys);
        }
Пример #6
0
        public bool InsertManyExtra()
        {
            Random           random     = new Random(125125);
            SqlTypeInfo      info       = ExtraCrud.TypeInfo <Employee>();
            List <SqlColumn> columns    = info.Columns.Where(c => !c.IsAutoKey).ToList();
            int               max       = (2050 / columns.Count) * columns.Count;
            StringBuilder     sb        = new StringBuilder("INSERT INTO [dbo].[Employees] (" + string.Join(",", columns.Select(c => c.ColumnName)) + @") VALUES 
");
            DynamicParameters dynParams = new DynamicParameters();

            for (int j = 0; j < max;)
            {
                sb.Append('(');
                Employee employee = Employee.Create(random);
                for (int k = 0; k < columns.Count; k++, j++)
                {
                    string name = "@p" + j;
                    sb.Append(name).Append(',');
                    dynParams.Add(name, columns[k].Getter(employee));
                }
                sb.Remove(sb.Length - 1, 1).Append(@"),");
            }
            string cmd = sb.Remove(sb.Length - 1, 1).ToString();

            using (SqlConnection conn = new SqlConnection(ConnString)) {
                for (int i = 0; i < Count; i += max)
                {
                    conn.Execute(cmd, dynParams);
                }
                try {
                    conn.Truncate <Employee>();
                }
                catch {
                    conn.DeleteList <Employee>();
                }
            }
            return(true);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="DataAccessObject{T}"/> class.
 /// </summary>
 /// <param name="connectionString">The connectionString<see cref="string"/></param>
 /// <param name="buffered">The buffered<see cref="bool"/></param>
 public DataAccessObject(string connectionString, bool buffered = true)
 {
     Connection = new SqlConnection(connectionString);
     Buffered   = buffered;
     Queries    = ExtraCrud.Queries <T>();
 }
Пример #8
0
 static Inserts()
 {
     _ = ExtraCrud.Builder <Employee>();
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="DataAccessObject{T}"/> class.
 /// </summary>
 /// <param name="connectionString">The connectionString<see cref="string"/></param>
 public DataAccessObject(string connectionString)
 {
     Connection = new SqlConnection(connectionString);
     Queries    = ExtraCrud.Queries <T>();
 }