예제 #1
0
        public OnConflictDoUpdate(IInsert <T1> insert, Expression <Func <T1, object> > columns = null)
        {
            _pgsqlInsert = insert as PostgreSQLInsert <T1>;
            if (_pgsqlInsert == null)
            {
                throw new Exception("OnConflictDoUpdate 是 FreeSql.Provider.PostgreSQL 特有的功能");
            }

            if (columns != null)
            {
                var colsList = new List <ColumnInfo>();
                var cols     = _pgsqlInsert.InternalCommonExpression.ExpressionSelectColumns_MemberAccess_New_NewArrayInit(null, columns?.Body, false, null).ToDictionary(a => a, a => true);
                foreach (var col in _pgsqlInsert.InternalTable.Columns.Values)
                {
                    if (cols.ContainsKey(col.Attribute.Name))
                    {
                        colsList.Add(col);
                    }
                }
                _columns = colsList.ToArray();
            }
            if (_columns == null || _columns.Any() == false)
            {
                _columns = _pgsqlInsert.InternalTable.Primarys;
            }
            if (_columns.Any() == false)
            {
                throw new Exception("OnConflictDoUpdate 功能要求实体类必须设置 IsPrimary 属性");
            }
        }
예제 #2
0
        public OnConflictDoUpdate(IInsert <T1> insert, Expression <Func <T1, object> > columns = null)
        {
            _pgsqlInsert = insert as PostgreSQLInsert <T1>;
            if (_pgsqlInsert == null)
            {
                throw new Exception(CoreStrings.S_Features_Unique("OnConflictDoUpdate", "PostgreSQL"));
            }
            if (_pgsqlInsert._noneParameterFlag == "c")
            {
                _pgsqlInsert._noneParameterFlag = "cu";
            }

            if (columns != null)
            {
                var colsList = new List <ColumnInfo>();
                var cols     = _pgsqlInsert.InternalCommonExpression.ExpressionSelectColumns_MemberAccess_New_NewArrayInit(null, null, columns?.Body, false, null).ToDictionary(a => a, a => true);
                foreach (var col in _pgsqlInsert.InternalTable.Columns.Values)
                {
                    if (cols.ContainsKey(col.Attribute.Name))
                    {
                        colsList.Add(col);
                    }
                }
                _tempPrimarys = colsList.ToArray();
            }
            if (_tempPrimarys == null || _tempPrimarys.Any() == false)
            {
                _tempPrimarys = _pgsqlInsert.InternalTable.Primarys;
            }
            if (_tempPrimarys.Any() == false)
            {
                throw new Exception(CoreStrings.S_OnConflictDoUpdate_MustIsPrimary);
            }
        }