Exemple #1
0
        public override void ProcessRecord(DataRecord record, RecordAccepter accepter)
        {
            if (_wrapper == null) {
                bool hasHeaders = false;

                HintMessageToConsole();

                if (record is TableRecord) {
                    _wrapper = new ColumnWrapper(record as TableRecord);
                    _keyColumnNos = _wrapper.GetColumnNos(KeyExpression);
                }

                else {
                    hasHeaders = ColumnWrapper.HasHeaders(KeyExpression);
                    _wrapper = new ColumnWrapper(record, _sourceName, _separator, hasHeaders);
                    _keyColumnNos = _wrapper.GetColumnNos(KeyExpression);
                }

                // if the countColumnExpression ends in a bang! we allow ulong.Parse errors.
                // otherwise we abort on an error.

                if (_countExpression != null && _countExpression.EndsWith("!")) {
                    _interpretAsZero = true;
                    _countExpression = _countExpression.TrimEnd('!');
                }

                if (_countExpression != null) {
                    int[] countColumnNos = _wrapper.GetColumnNos(_countExpression);
                    if (countColumnNos.Length != 1) {
                        Console.WriteLine("Illegal Count Column expression");
                        Environment.Exit(1);
                    }

                    _countColumnNo = countColumnNos[0];
                    if (_countColumnNo > _wrapper.ColumnNames.Length - 1) {
                        Console.WriteLine("Illegal Count Column expression");
                        Environment.Exit(1);
                    }
                }

                // if countRecord and count column is last column.
                if (record is CountRecord && _countColumnNo == _wrapper.ColumnNames.Length - 1) {
                    _caseNum = 0;
                }

                // if no expression given use 1
                else if (_countExpression == null) {
                    _caseNum = 1;
                }

                else _caseNum = 2;

                if (hasHeaders) return; // eat up this record containing headers
            }

            // cases:
            // 0 : record = CountRecord && countColumn refers to the count.
            // 1 : countExpression == null.  Just 1-count the keys
            // 2 : everything else

            // not sure if this is the best way to ignore blank lines coming in.
            if (record.Key.Length == 0) return;

            CountRecord outRecord = new CountRecord();
            _wrapper.SetRecord(record);

            // build the key
            _stringBuilder.Length = 0;

            for (int i = 0; i < _keyColumnNos.Length; i++) {
                if (i != 0) _stringBuilder.Append(_separator);
                _wrapper.AppendColumn(_keyColumnNos[i], _stringBuilder);
            }

            outRecord.Key = _stringBuilder.ToString();

            // we special case 0, because then we can avoid converting from ulong to string
            // and back to ulong.
            switch (_caseNum) {
                case 0:
                    outRecord.Count = ((CountRecord)record).Count;
                    break;

                case 1:
                    outRecord.Count = 1;
                    break;

                case 2:
                    _stringBuilder.Length = 0;
                    _wrapper.AppendColumn(_countColumnNo, _stringBuilder);
                    try {
                        outRecord.Count = ulong.Parse(_stringBuilder.ToString());
                    }
                    catch {
                        if (!_interpretAsZero) {
                            Console.WriteLine("Illegal ulong string '{0}'.\nTo interpret as zero: count column expression = ${1}!", _stringBuilder.ToString(), _countColumnNo+1);
                            Environment.Exit(-1);
                        }

                        outRecord.Count = 0;

                        _numParseErrors++;
                        //return; // abort this record
                    }
                    break;
            }

            accepter.AddRecord(outRecord);
        }