Ejemplo n.º 1
0
 public SequenceInfo FindOrCreateBookSequence(SequenceInfoNode info)
 {
     return FindOrCreateBookSequence(info, null);
 }
Ejemplo n.º 2
0
        public SequenceInfo FindOrCreateBookSequence(SequenceInfoNode info, IDbTransaction transaction)
        {
            string commandText;
            FbCommand command;
            SequenceInfo sequence;

            if (info == null)
            {
                throw new ArgumentNullException("info");
            }

            this.manager.BeginConnect();

            try
            {
                commandText =
                    @"SELECT sequences.* FROM sequences
                      WHERE UPPERCASE(sequences.""SEQUENCE"") = UPPERCASE(@sequencename)";

                command = this.connection.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = commandText;
                command.Transaction = transaction as FbTransaction;

                command.Parameters.Add("@sequencename", FbDbType.VarChar).Value = info.Name;

                using (SqlDbCommand dbCommand = new SqlDbCommand(command))
                {
                    sequence = dbCommand.ExecuteObject<SequenceInfo>();
                }

                if (sequence != null)
                {
                    sequence.SequenceNumber = info.Number;
                    return sequence;
                }

                sequence = new SequenceInfo();
                sequence.SequenceName = StringUtils.Truncate(info.Name, 125);

                commandText =
                    "INSERT INTO SEQUENCES (SEQUENCEID, \"SEQUENCE\") " +
                    "VALUES (GEN_ID(GEN_SEQUENCES_ID, 1), @sequencename) " +
                    "RETURNING SEQUENCEID";

                using (FbCommand cmd = this.connection.CreateCommand())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = commandText;
                    cmd.Transaction = transaction as FbTransaction;

                    cmd.Parameters.Add("@sequencename", FbDbType.VarChar, 125).Value = sequence.SequenceName ?? String.Empty;

                    cmd.Parameters.Add("@sequenceid", FbDbType.Integer).Direction = ParameterDirection.Output;
                    cmd.ExecuteNonQuery();

                    sequence.SequenceId = Convert.ToInt32(cmd.Parameters["@sequenceid"].Value);
                }

                sequence.SequenceNumber = info.Number;
            }
            catch (FbException exp)
            {
                throw new DatabaseException(exp.Message, exp);
            }
            finally
            {
                this.manager.EndConnect();
            }

            return sequence;
        }