예제 #1
0
    public ContentProperty(DataType _type, string _name, object _value)
    {
        DataType[] flattypes = new DataType[] { DataType.Currency, DataType.DateTime,
                                                DataType.Int, DataType.String, DataType.Text };

        Name = _name;

        if (flattypes.Contains(_type))
        {
            Value = _value;
        }
        else
        {
            switch (_type)
            {
            case DataType.Binary:
                Value = ((BinaryData)_value).FileName.FullFileName;
                break;

            case DataType.Reference:
                //Value = ((IEnumerable<Node>)_value).Select<Node, int>(n => n.Id).ToArray();
                Value = "";
                break;
            }
        }
    }
예제 #2
0
파일: TypeUnifier.cs 프로젝트: plioi/rook
        public IEnumerable<string> Unify(DataType a, DataType b)
        {
            a = Normalize(a);
            b = Normalize(b);

            if (a is TypeVariable)
            {
                var variableA = (TypeVariable)a;

                if (b.Contains(variableA))
                {
                    if (a != b)
                        return Error(a, b);

                    return success;
                }

                substitutions[variableA] = b;
                return success;
            }

            if (b is TypeVariable)
                return Unify(b, a);

            if (a.Name != b.Name)
                return Error(a, b);

            if (a.GenericArguments.Count() != b.GenericArguments.Count())
                return Error(a, b);

            return PairwiseUnify(a.GenericArguments, b.GenericArguments);
        }
예제 #3
0
    public ContentProperty(DataType _type, string _name, object _value)
    {
        DataType[] flattypes = new DataType[] { DataType.Currency, DataType.DateTime,
                                                DataType.Int, DataType.String, DataType.Text };

        Name = _name;

        if (flattypes.Contains(_type))
        {
            Value = _value;
        }
        else
        {
            switch (_type)
            {
            case DataType.Binary:
                Value = ((BinaryData)_value).FileName.FullFileName;
                break;

            case DataType.Reference:
                Value = "";
                break;
            }
        }
    }
예제 #4
0
        /// <summary>
        /// Parses a byte array to a RowValue2 for this column data type. This method assumes the byte array passed in is the value. If the column length is variable, you must ensure the array size passed in
        /// is the array for the total value (do not include the size prefix for variable length columns.)
        /// </summary>
        /// <param name="span">The byte array to parse. This array must contain the value to be parsed. Ensure that it does not contain the size prefix for variable length columns.</param>
        /// <returns>A RowValue2</returns>
        public RowValue2 Parse(Span <byte> span)
        {
            var value = new RowValue2();

            value.Column = this;

            if (!isVariableLengthColumn())
            {
                if (DataType.Equals("DATETIME"))
                {
                    value.Value = DatabaseBinaryConverter.BinaryToDateTime(span).ToString();
                }

                if (DataType.Equals("BIT"))
                {
                    value.Value = DatabaseBinaryConverter.BinaryToBoolean(span).ToString();
                }

                if (DataType.Equals("INT"))
                {
                    value.Value = DatabaseBinaryConverter.BinaryToInt(span).ToString();
                }

                if (DataType.Contains("CHAR"))
                {
                    // need to get the fixed character width, then parse
                    // CHAR always pads any values less with whitespace
                    throw new NotImplementedException();
                }
            }
            else
            {
                // if it's varaible length, then the array has prefix'ed the actual value with the size (length) of the item. The prefix is the size of an INT32.
                // example VARCHAR(10)
                // byte format is 2 <byte array>
                // in the above if the value was "XX" it means that we only used 2 characters out of a potential maximum of 10 characters wide

                if (DataType.Contains("VARCHAR"))
                {
                    throw new NotImplementedException();
                }

                if (DataType.Contains("DECIMAL"))
                {
                    throw new NotImplementedException();
                }

                if (DataType.Contains("NUMERIC"))
                {
                    throw new NotImplementedException();
                }
            }

            return(value);
        }
예제 #5
0
        private void CleanDataType()
        {
            if (DataType.Contains("char") || DataType.Contains("binary"))
            {
                return;
            }
            int index = DataType.IndexOf("(");

            if (index == -1)
            {
                return;
            }
            DataType = DataType.Substring(0, index);
        }
예제 #6
0
            public bool RequiresUpdate()
            {
                if (DataType.Contains("text"))
                {
                    return(false);
                }

                if (!DataType.Contains("char"))
                {
                    return(true);
                }

                if (!Length.HasValue || Length.Value < 255)
                {
                    return(true);
                }

                return(false);
            }
예제 #7
0
    public ContentProperty(DataType _type, string _name, object _value)
    {
        DataType[] flattypes = new DataType[] {DataType.Currency, DataType.DateTime, 
                    DataType.Int, DataType.String, DataType.Text };

        Name = _name;

        if (flattypes.Contains(_type))
            Value = _value;
        else
            switch (_type)
            {
                case DataType.Binary:
                    Value = ((BinaryData)_value).FileName.FullFileName;
                    break;
                case DataType.Reference:
                    //Value = ((IEnumerable<Node>)_value).Select<Node, int>(n => n.Id).ToArray();
                    Value = "";
                    break;
            }
    }
예제 #8
0
        public static bool IsNumeric(DataType dataType)
        {
            bool isNumeric = false;

            var numericTypes = new DataType[] {
                DataType.Byte,
                DataType.Decimal,
                DataType.Double,
                DataType.Int16,
                DataType.Int32,
                DataType.Int64,
                DataType.Single,
            };

            if (numericTypes.Contains(dataType))
            {
                isNumeric = true;
            }

            return(isNumeric);
        }
예제 #9
0
        private string SqlLiteral(string input)
        {
            string result = input;

            string quote(string value)
            {
                return("'" + value + "'");
            };

            if (DataType.Contains("char"))
            {
                result = result.Replace("'", "''");
                result = quote(result);
            }

            if (DataType.Contains("date"))
            {
                result = quote(result);
            }

            return(result);
        }
예제 #10
0
        /// <summary>
        /// Returns the byte length for this column's data type
        /// </summary>
        /// <returns>The length of bytes for this column</returns>
        private int GetSizeForDataType()
        {
            int size = 0;

            if (DataType.Equals("BIT"))
            {
                size = DatabaseConstants.SIZE_OF_BOOL;
            }

            if (DataType.Equals("DATETIME"))
            {
                size = DatabaseConstants.SIZE_OF_DATETIME;
            }

            if (DataType.Equals("INT"))
            {
                size = DatabaseConstants.SIZE_OF_INT;
            }

            if (DataType.Contains("CHAR"))
            {
                // need to get the fixed character width, then parse
                throw new NotImplementedException();
            }

            if (DataType.Contains("DECIMAL"))
            {
                throw new NotImplementedException();
            }

            if (DataType.Contains("NUMERIC"))
            {
                throw new NotImplementedException();
            }

            return(size);
        }
예제 #11
0
        /// <summary>
        /// Parse data to readable string
        /// </summary>
        /// <param name="Data">byte[]</param>
        /// <param name="cDataCount">byte</param>
        /// <param name="Outputs">IDictionary</param>
        /// <returns></returns>
        private String ParseData(byte[] Data, byte cDataCount,
                                 IDictionary <String, String> Outputs)
        {
            String Outs = String.Empty;
            String Name;
            String DataType;
            String Value;
            int    nIndex   = 0;
            bool   bSuccess = true;

            do
            {
                foreach (KeyValuePair <String, String> pair in Outputs)
                {
                    // Check if  failed to parse or more outputs than the data
                    if (!bSuccess || (nIndex >= (int)cDataCount))
                    {
                        break;
                    }

                    String[] Values = pair.Value.Split(',');
                    if (Values.Length < 2)
                    {
                        break;
                    }

                    Name     = Values[0];
                    Value    = String.Empty;
                    DataType = Values[1].Trim();

                    if (DataType.IndexOf("HEX:", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        String[] Val   = Values[1].Split(':');
                        int      nSize = Convert.ToInt32(Val[1]);
                        for (int i = 0; i < nSize; i++)
                        {
                            Value += String.Format("0x{0:X2}", Data[nIndex + i]);
                        }
                        nIndex += nSize;
                    }
                    else if ((DataType.IndexOf("AS:", StringComparison.OrdinalIgnoreCase) == 0) ||
                             (DataType.IndexOf("ASCII:", StringComparison.OrdinalIgnoreCase) == 0))
                    {
                        String[] Ascii = Values[1].Split(':');
                        int      nSize = Convert.ToInt32(Ascii[1]);
                        for (int i = 0; i < nSize; i++)
                        {
                            char ch = (char)(Data[nIndex + i] & 0x00ff);
                            if (ch == 0)
                            {
                                break;
                            }

                            Value += ch;
                        }
                        Value   = Value.Trim();
                        nIndex += nSize;
                    }
                    else if ((DataType.IndexOf("PA:", StringComparison.OrdinalIgnoreCase) == 0) ||
                             (DataType.IndexOf("PACKED ASCII:", StringComparison.OrdinalIgnoreCase) == 0))
                    {
                        String[] Pa    = Values[1].Split(':');
                        int      nSize = Convert.ToInt32(Pa[1]);
                        byte[]   Temp  = new byte[nSize];
                        for (int i = 0; i < nSize; i++)
                        {
                            Temp[i] = Data[nIndex + i];
                        }
                        Value   = (HartUtil.UnpackAscii(Temp, (byte)nSize)).Trim();
                        nIndex += nSize;
                    }
                    else
                    {
                        switch (DataType)
                        {
                        case "BITS8":
                        {
                            byte[] c = new byte[1];
                            c[0] = Data[nIndex++];
                            BitArray bits = new BitArray(c);
                            for (int i = 0; i < bits.Length; i++)
                            {
                                Value += ((bits[i]) ? 1 : 0);
                            }
                            break;
                        }

                        case "BL":
                        case "BOOLEAN":
                        case "UNSIGNED8":
                        case "U8":
                        {
                            byte c = Data[nIndex++];
                            Value = c.ToString();
                            break;
                        }

                        case "SIGNED8":
                        case "S8":
                        {
                            sbyte c = (sbyte)(Data[nIndex++]);
                            Value = c.ToString();
                            break;
                        }

                        case "UNSIGNED16":
                        case "U16":
                        {
                            ushort sh = (ushort)((Data[nIndex] << 8) + (Data[nIndex + 1] & 0x0ff));
                            nIndex += 2;
                            Value   = sh.ToString();
                            break;
                        }

                        case "SIGNED16":
                        case "S16":
                        {
                            short sh = (short)((Data[nIndex] << 8) + (Data[nIndex + 1] & 0x0ff));
                            nIndex += 2;
                            Value   = sh.ToString();
                            break;
                        }

                        case "UNSIGNED24":
                        case "U24":
                        {
                            uint ui = (uint)((Data[nIndex] << 16) + (Data[nIndex + 1] << 8) +
                                             (Data[nIndex + 2] & 0x0ff));
                            nIndex += 3;
                            Value   = ui.ToString();
                            break;
                        }

                        case "SIGNED24":
                        case "S24":
                        case "RAW24":
                        case "R24":
                        {
                            int i = (int)((Data[nIndex] << 16) + (Data[nIndex + 1] << 8) +
                                          (Data[nIndex + 2] & 0x0ff));
                            nIndex += 3;
                            Value   = i.ToString();
                            break;
                        }

                        case "UNSIGNED32":
                        case "U32":
                        {
                            uint i = (uint)((Data[nIndex] << 24) + (Data[nIndex + 1] << 16) +
                                            (Data[nIndex + 2] << 8) + (Data[nIndex + 3] & 0x0ff));
                            nIndex += 4;
                            Value   = String.Format("{0}", i);
                            break;
                        }

                        case "UNSIGNED40":
                        case "U40":
                        {
                            UInt64 i = (uint)((Data[nIndex] << 32) + (Data[nIndex + 1] << 24) +
                                              (Data[nIndex + 2] << 16) + (Data[nIndex + 3] << 8) + (Data[nIndex + 4] & 0x0ff));
                            nIndex += 5;
                            Value   = String.Format("{0}", i);
                            break;
                        }

                        case "SIGNED32":
                        case "S32":
                        case "RAW32":
                        case "R32":
                        {
                            int i = (int)((Data[nIndex] << 24) + (Data[nIndex + 1] << 16) +
                                          (Data[nIndex + 2] << 8) + (Data[nIndex + 3] & 0x0ff));
                            nIndex += 4;
                            Value   = String.Format("{0}", i);
                            break;
                        }

                        case "FLOAT":
                        case "FL":
                        case "F32":
                        {
                            byte[] arr = new byte[4];
                            for (int i = 0, j = 3; i < 4; i++, j--)
                            {
                                arr[i] = Data[nIndex + j];
                            }
                            float fl = BitConverter.ToSingle(arr, 0);
                            nIndex += 4;
                            Value   = String.Format("{0}", fl);
                            break;
                        }

                        case "LONG":
                        case "LONG:TIME":
                        {
                            byte[] arr = new byte[8];
                            for (int i = 0, j = 7; i < 8; i++, j--)
                            {
                                arr[i] = Data[nIndex + j];
                            }
                            long l = BitConverter.ToInt64(arr, 0);
                            nIndex += 8;

                            if (DataType.Contains(":TIME"))
                            {
                                // value is in milliseconds since 1/1/1970
                                DateTime started   = new DateTime(1970, 1, 1, 0, 0, 0);
                                DateTime timeOfday = started.AddMilliseconds((double)l);
                                Value = timeOfday.ToString("MM/dd/yy HH:mm:ss.fff", DateTimeFormatInfo.InvariantInfo);
                            }
                            else
                            {
                                Value = String.Format("{0}", l);
                            }

                            break;
                        }

                        case "T12":
                        case "T24":
                        case "TIME":
                        case "TIME_12":
                        case "TIME-12":
                        case "TIME_12H":
                        case "TIME-12H":
                        case "TIME_24":
                        case "TIME-24":
                        case "TIME_24H":
                        case "TIME-24H":
                        {
                            uint t = (uint)((Data[nIndex] << 24) + (Data[nIndex + 1] << 16) +
                                            (Data[nIndex + 2] << 8) + (Data[nIndex + 3] & 0x0ff));
                            // calcuate ms in 1/32s of a millisecond
                            t /= 32;
                            uint ms = t % 1000;
                            t /= 1000;
                            uint secs = t % 60;
                            t /= 60;
                            uint mins = t % 60;
                            uint hrs  = (uint)(t / 60);
                            bool b24h = false;

                            // Check if value is in 24 hour display
                            if (DataType.Contains("24"))
                            {
                                b24h = true;
                            }

                            if (b24h)
                            {
                                Value = String.Format("{0:d2}:{1:d2}:{2:d2}.{3:d2}", hrs, mins, secs, ms);
                            }
                            else
                            {
                                if (hrs >= 12)
                                {
                                    Value = String.Format("{0:d2}:{1:d2}:{2:d2}.{3:d2} PM", (hrs - 12), mins, secs, ms);
                                }
                                else
                                {
                                    Value = String.Format("{0:d2}:{1:d2}:{2:d2}.{3:d2} AM", (hrs), mins, secs, ms);
                                }
                            }
                            nIndex += 4;
                            break;
                        }

                        default:
                            // unknown data type, stop parsing data
                            Value    = "???";
                            bSuccess = false;
                            break;
                        }
                    }

                    Outs += (String.Format("\r\n{0}={1}", Name, Value));
                }

                // unknown data bytes or have more bytes than output parameters
                if (nIndex < (int)cDataCount)
                {
                    Outs += "\r\nUndefined data bytes=";
                    for (int i = nIndex; i < cDataCount; i++)
                    {
                        Outs += String.Format("{0:X2}", Data[i]);
                    }
                }
            } while (false); /* ONCE */
            return(Outs);
        }