|
| 1 | +using System.Data; |
| 2 | + |
| 3 | +namespace FAST.FBasicInterpreter |
| 4 | +{ |
| 5 | + /// <summary> |
| 6 | + /// Abstract class to implement any collection based on a data reader |
| 7 | + /// </summary> |
| 8 | + /// <typeparam name="TReader">The type of the reader</typeparam> |
| 9 | + /// <typeparam name="TCallback">The type of FBASIC data adapter callback</typeparam> |
| 10 | + public abstract class dataReaderCollection<TReader, TCallback> : IBasicCollection |
| 11 | + where TReader : IDataReader |
| 12 | + where TCallback : IfbasicDataAdapter |
| 13 | + { |
| 14 | + public bool _endOfData = false; |
| 15 | + public bool endOfData |
| 16 | + { |
| 17 | + get => _endOfData; |
| 18 | + set => _endOfData = value; |
| 19 | + } |
| 20 | + |
| 21 | + public abstract string title { get; } |
| 22 | + |
| 23 | + public TReader reader; |
| 24 | + |
| 25 | + private bool isOpen = false; |
| 26 | + protected TCallback callback; |
| 27 | + private string cursorName = null; |
| 28 | + private Dictionary<string, Tuple<int, Type, bool>> columnsMap = null; |
| 29 | + |
| 30 | + public dataReaderCollection(string cursorName, TCallback callback) |
| 31 | + { |
| 32 | + this.callback = callback; |
| 33 | + this.cursorName = cursorName; |
| 34 | + inner_reset(); |
| 35 | + } |
| 36 | + |
| 37 | + public object Current { get; set; } = null; |
| 38 | + |
| 39 | + protected abstract bool openCollection(string name); |
| 40 | + protected abstract void closeCollection(string name); |
| 41 | + protected abstract bool readerHasRows { get; } |
| 42 | + |
| 43 | + |
| 44 | + public bool MoveNext() |
| 45 | + { |
| 46 | + if (!this.isOpen) |
| 47 | + { |
| 48 | + this.isOpen = openCollection(cursorName); //callback.openCursor(cursorName, this); |
| 49 | + if (!this.isOpen) return false; //moveToNext=false |
| 50 | + this.columnsMap = getColumnsMap(reader); |
| 51 | + } |
| 52 | + |
| 53 | + // (v) code to MoveNext() |
| 54 | + if (!readerHasRows) return false; //moveToNext=false; |
| 55 | + this.Current = reader; |
| 56 | + |
| 57 | + this.endOfData = !reader.Read(); |
| 58 | + |
| 59 | + return !this.endOfData; |
| 60 | + } |
| 61 | + |
| 62 | + public void Reset() |
| 63 | + { |
| 64 | + if (this.isOpen) |
| 65 | + { |
| 66 | + closeCollection(cursorName); //callback.closeCursor(cursorName, this); |
| 67 | + this.isOpen = false; |
| 68 | + } |
| 69 | + |
| 70 | + inner_reset(); |
| 71 | + } |
| 72 | + |
| 73 | + private void inner_reset() |
| 74 | + { |
| 75 | + this.endOfData = false; |
| 76 | + this.isOpen = false; |
| 77 | + } |
| 78 | + |
| 79 | + public Value getValue(string name) |
| 80 | + { |
| 81 | + if (!this.columnsMap.ContainsKey(name)) callback.Error(title, $"Name: {name} is missing from {cursorName} [S003]"); |
| 82 | + var value = reader[name]; |
| 83 | + var isNumeric = columnsMap[name].Item3; |
| 84 | + if (isNumeric) |
| 85 | + { |
| 86 | + return new Value(Convert.ToDouble(value)); |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + return new Value(value.ToString()); |
| 91 | + } |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// A map between column name and ordinary and type |
| 98 | + /// of each column, extracted from a reader |
| 99 | + /// MAP: Key=Column Name, Value=int:Ordinary Number, type:The Type, bool:true if type is Numeric |
| 100 | + /// </summary> |
| 101 | + /// <param name="reader">the reader</param> |
| 102 | + /// <returns>Dictionary with column names as key and Tuple as value</returns |
| 103 | + private static Dictionary<string, Tuple<int, Type, bool>> getColumnsMap(IDataReader reader) |
| 104 | + { |
| 105 | + if (reader == null) { throw new ArgumentNullException("Class isn't yet bind with a DbDataReader object"); } |
| 106 | + |
| 107 | + Dictionary<string, Tuple<int, Type, bool>> map = new(); |
| 108 | + |
| 109 | + for (int field = 0; field < reader.FieldCount; field++) |
| 110 | + { |
| 111 | + var type = reader.GetFieldType(field); |
| 112 | + map.Add(reader.GetName(field), new Tuple<int, Type, bool>(field, type, isNumericType(type))); |
| 113 | + } |
| 114 | + return map; |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Check if a type is numeric type |
| 119 | + /// </summary> |
| 120 | + /// <param name="type">the type to check</param> |
| 121 | + /// <returns>True if it is numeric type</returns> |
| 122 | + private static bool isNumericType(Type type) |
| 123 | + { |
| 124 | + switch (Type.GetTypeCode(type)) |
| 125 | + { |
| 126 | + case TypeCode.Byte: |
| 127 | + case TypeCode.SByte: |
| 128 | + case TypeCode.UInt16: |
| 129 | + case TypeCode.UInt32: |
| 130 | + case TypeCode.UInt64: |
| 131 | + case TypeCode.Int16: |
| 132 | + case TypeCode.Int32: |
| 133 | + case TypeCode.Int64: |
| 134 | + case TypeCode.Decimal: |
| 135 | + case TypeCode.Double: |
| 136 | + case TypeCode.Single: |
| 137 | + return true; |
| 138 | + default: |
| 139 | + return false; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + } |
| 144 | +} |
0 commit comments