コード例 #1
0
ファイル: UpsertBuilder.cs プロジェクト: Fax/dblinq2007
        public BulkQuery GetExistsQuery <T>(DataContext dataContext)
        {
            BulkQuery result = null;

            if (ExistsQueryCache.TryGetValue(typeof(T), out result))
            {
                if (result.DataContext == dataContext)
                {
                    return(result);
                }
            }

            var upsertParameters = GetUpsertParameters <T>(true, Columns <T>(), dataContext);
            var sqlProvider      = dataContext.Vendor.SqlProvider;
            var sqlBuilder       = new SqlStatementBuilder("SELECT COUNT (*) FROM ");

            sqlBuilder.Append(sqlProvider.GetTable(upsertParameters.Table.TableName));
            sqlBuilder.Append(" WHERE ");
            var valueSet = false;

            for (IEnumerator <SqlStatement> column = upsertParameters.InputPKColumns.GetEnumerator(), value = upsertParameters.InputPKValues.GetEnumerator(); column.MoveNext() && value.MoveNext();)
            {
                if (valueSet)
                {
                    sqlBuilder.Append(" AND ");
                }
                sqlBuilder.AppendFormat("{0} = {1}", column.Current, value.Current);
                valueSet = true;
            }

            result = new BulkQuery {
                DataContext          = dataContext,
                Sql                  = sqlBuilder.ToSqlStatement(),
                InputParameters      = upsertParameters.PKParameters,
                PrimaryKeyParameters = upsertParameters.PKParameters
            };
            ExistsQueryCache[typeof(T)] = result;
            return(result);
        }
コード例 #2
0
ファイル: UpsertBuilder.cs プロジェクト: Fax/dblinq2007
        public virtual int Bulk <T>(DataContext dataContext, IEnumerable <T> entities, BulkQuery query)
        {
            var result = 0;

            var command = query.GetCommandTransactional(true);

            WriteLog(dataContext, command.Command.CommandText);
            foreach (var item in entities)
            {
                try {
                    query.SetParameterValues(item, command.Command);
                    command.Command.ExecuteNonQuery();
                    result++;
                } catch (Exception ex) {
                    var caller = new System.Diagnostics.StackTrace().GetFrame(1).GetMethod().Name;
                    WriteLog(dataContext, string.Format("Error in {0}:\t {1}", caller, ParametersToString <T>(command)));
                    throw ex;
                }
            }
            Commit(command);

            return(result);
        }