Example #1
0
        private void ExecuteDelete(IChangeSet owner, IChangeSetRow row, NpgsqlConnection connection, NpgsqlTransaction transaction)
        {
            Table tbl = owner?.Table;

            if (tbl == null)
            {
                throw new ApplicationException("更新対象の表が設定されていないため、削除できません");
            }
            if (owner.KeyFields == null || owner.KeyFields.Length == 0)
            {
                throw new ApplicationException("主キーが設定されていないため、削除できません");
            }
            List <NpgsqlParameter> listP = new List <NpgsqlParameter>();
            StringBuilder          bufC  = new StringBuilder();
            bool needAnd = false;

            foreach (ColumnInfo f in owner.KeyFields)
            {
                if (needAnd)
                {
                    bufC.Append("  and ");
                }
                else
                {
                    bufC.Append("where ");
                }
                if (f.IsNullable)
                {
                    bufC.AppendFormat("(({0} = :old_{1}) or ({0} is null and :old_{1} is null))", GetEscapedIdentifier(f.Name, true), f.Name);
                }
                else
                {
                    bufC.AppendFormat("({0} = :old_{1})", GetEscapedIdentifier(f.Name, true), f.Name);
                }
                bufC.AppendLine();
                NpgsqlParameter p = CreateParameterByFieldInfo(f, row.Old(f.Index), true) as NpgsqlParameter;
                listP.Add(p);
                needAnd = true;
            }

            StringBuilder buf = new StringBuilder();

            buf.Append("delete from ");
            buf.AppendLine(tbl.EscapedIdentifier(CurrentSchema));
            buf.Append(bufC);
            ExecuteSql(buf.ToString(), listP.ToArray(), owner, row, connection, transaction);
        }
Example #2
0
        private void ExecuteUpdate(IChangeSet owner, IChangeSetRow row, NpgsqlConnection connection, NpgsqlTransaction transaction)
        {
            Table tbl = owner?.Table;

            if (tbl == null)
            {
                throw new ApplicationException("更新対象の表が設定されていないため、更新できません");
            }
            if (owner.KeyFields == null || owner.KeyFields.Length == 0)
            {
                throw new ApplicationException("主キーが設定されていないため、更新できません");
            }
            StringBuilder          bufF  = new StringBuilder();
            List <NpgsqlParameter> listP = new List <NpgsqlParameter>();
            bool needComma = false;

            foreach (ColumnInfo f in owner.Fields)
            {
                if (!row.IsModified(f.Index))
                {
                    continue;
                }
                if (needComma)
                {
                    bufF.AppendLine(", ");
                }
                bufF.Append("  ");
                bufF.Append(GetEscapedIdentifier(f.Name, true));
                bufF.Append(" = :");
                bufF.Append(f.Name);
                NpgsqlParameter p = CreateParameterByFieldInfo(f, row[f.Index], false) as NpgsqlParameter;
                p.SourceColumn  = f.Name;
                p.SourceVersion = DataRowVersion.Current;
                listP.Add(p);
                needComma = true;
            }
            bufF.AppendLine();
            if (!needComma)
            {
                // 変更がなかった
                return;
            }
            StringBuilder bufC    = new StringBuilder();
            bool          needAnd = false;

            foreach (ColumnInfo f in owner.KeyFields)
            {
                if (needAnd)
                {
                    bufC.Append("  and ");
                }
                else
                {
                    bufC.Append("where ");
                }
                if (f.IsNullable)
                {
                    bufC.AppendFormat("(({0} = :old_{1}) or ({0} is null and :old_{1} is null))", GetEscapedIdentifier(f.Name, true), f.Name);
                }
                else
                {
                    bufC.AppendFormat("({0} = :old_{1})", GetEscapedIdentifier(f.Name, true), f.Name);
                }
                bufC.AppendLine();
                NpgsqlParameter p = CreateParameterByFieldInfo(f, row.Old(f.Index), true) as NpgsqlParameter;
                listP.Add(p);
                needAnd = true;
            }

            StringBuilder buf = new StringBuilder();

            buf.Append("update ");
            buf.Append(tbl.EscapedIdentifier(CurrentSchema));
            buf.AppendLine(" set");
            buf.Append(bufF);
            buf.Append(bufC);
            buf.Append("returning *");
            ExecuteSql(buf.ToString(), listP.ToArray(), owner, row, connection, transaction);
        }