Azavea.Open.DAO.CSV.CsvDaLayer.Delete C# (CSharp) Méthode

Delete() public méthode

Deletes a data object record using the mapping and criteria for what's deleted.
public Delete ( ITransaction transaction, ClassMapping mapping, DaoCriteria crit ) : int
transaction ITransaction Should be null, transactions are not supported.
mapping ClassMapping The mapping of the table from which to delete.
crit Azavea.Open.DAO.Criteria.DaoCriteria Criteria for deletion. NOTE: Only the expressions are observed, /// other things (like "order" or start / limit) are ignored. /// WARNING: A null or empty (no expression) criteria will /// delete ALL records!
Résultat int
        public override int Delete(ITransaction transaction, ClassMapping mapping, DaoCriteria crit)
        {
            switch (_connDesc.Type)
            {
                case CsvConnectionType.Directory:
                case CsvConnectionType.FileName:
                    // These are OK.
                    break;
                default:
                    throw new LoggingException("Connection does not support deleting: " + _connDesc);
            }

            // No way to selectively delete text from a text file, so instead we copy all
            // the rows that don't match the criteria into a new file.
            string existingFile = GetFileName(mapping);
            string newFile = existingFile + ".new";
            DaoCriteria inverseCrit = new DaoCriteria();
            foreach (IExpression expr in crit.Expressions)
            {
                inverseCrit.Expressions.Add(expr.Invert());
            }
            CsvDataReader reader = new CsvDataReader(this, mapping, inverseCrit);
            try
            {
                TextWriter newWriter = new StreamWriter(newFile, false);
                try
                {
                    newWriter.WriteLine(MakeHeaderRow(mapping));
                    int numCols = reader.FieldCount;
                    while (reader.Read())
                    {
                        for (int x = 0; x < numCols; x++)
                        {
                            if (x > 0)
                            {
                                newWriter.Write(",");
                            }
                            newWriter.Write(QuoteValue(reader.GetValue(x)));
                        }
                        newWriter.WriteLine();
                    }
                }
                finally
                {
                    newWriter.Close();
                }
            }
            finally
            {
                reader.Close();
            }
            // Now move the old file out of the way and replace it with the new one.
            File.Replace(newFile, existingFile, existingFile + ".old", true);
            return FastDAO<object>.UNKNOWN_NUM_ROWS;
        }