예제 #1
0
        private static void CreateFieldByType(StreamWriter writer, List<string> keys, Field field, string suffix = "")
        {
            if (field.Type == DataType.None)
                return;

            if (field.Key)
                keys.Add(field.Name);

            #region Type
            switch (field.Type)
            {
                case DataType.Long:
                    writer.WriteLine("    `{0}` BIGINT NOT NULL DEFAULT '0',", field.Name.ToLower() + suffix);
                    break;
                case DataType.Ulong:
                    writer.WriteLine("    `{0}` BIGINT UNSIGNED NOT NULL DEFAULT '0',", field.Name.ToLower() + suffix);
                    break;
                case DataType.Int:
                    writer.WriteLine("    `{0}` INT NOT NULL DEFAULT '0',", field.Name.ToLower() + suffix);
                    break;
                case DataType.Uint:
                    writer.WriteLine("    `{0}` INT UNSIGNED NOT NULL DEFAULT '0',", field.Name.ToLower() + suffix);
                    break;
                case DataType.Short:
                    writer.WriteLine("    `{0}` SMALLINT NOT NULL DEFAULT '0',", field.Name.ToLower() + suffix);
                    break;
                case DataType.Ushort:
                    writer.WriteLine("    `{0}` SMALLINT UNSIGNED NOT NULL DEFAULT '0',", field.Name.ToLower() + suffix);
                    break;
                case DataType.SByte:
                    writer.WriteLine("    `{0}` TINYINT NOT NULL DEFAULT '0',", field.Name.ToLower() + suffix);
                    break;
                case DataType.Byte:
                    writer.WriteLine("    `{0}` TINYINT UNSIGNED NOT NULL DEFAULT '0',", field.Name.ToLower() + suffix);
                    break;
                case DataType.Float:
                    writer.WriteLine("    `{0}` FLOAT NOT NULL DEFAULT '0',", field.Name.ToLower() + suffix);
                    break;
                case DataType.Double:
                    writer.WriteLine("    `{0}` DOUBLE NOT NULL DEFAULT '0',", field.Name.ToLower() + suffix);
                    break;
                case DataType.String:
                case DataType.String2:
                case DataType.Pstring:
                    {
                        var maxLen = (int)Math.Pow(2, field.Size);
                        if (maxLen > 8000 || maxLen <= 1)
                            writer.WriteLine("    `{0}` TEXT,", field.Name.ToLower() + suffix);
                        else
                            writer.WriteLine("    `{0}` VARCHAR({1}),", field.Name.ToLower() + suffix, maxLen);
                    } break;
                case DataType.List:
                    {
                        if (field.Size > 0)
                        {
                            var fname = field.Name.ToLower();
                            if (!char.IsDigit(fname[fname.Length - 1]))
                            {
                                if (suffix.Length > 0 && suffix[0] == '_')
                                    fname += suffix.Substring(1);
                                else
                                    fname += suffix;
                            }
                            else { fname += suffix; }

                            writer.WriteLine("    `{0}` INT NOT NULL DEFAULT '0',", fname);
                        }

                        for (int i = 0; i < field.Maxsize; ++i)
                        {
                            var m_suffix = suffix + "_" + (i + 1);
                            foreach (var element in field.Fields)
                                CreateFieldByType(writer, keys, element, m_suffix);
                        }
                    }
                    break;
                case DataType.StringList:
                    {
                        foreach (var subField in field.Fields)
                        {
                            var maxLen = (int)Math.Pow(2, subField.Size);

                            if (maxLen > 8000 || maxLen <= 1)
                                writer.WriteLine("    `{0}` TEXT,", subField.Name.ToLower() + suffix);
                            else
                                writer.WriteLine("    `{0}` VARCHAR({1}),", subField.Name.ToLower() + suffix, maxLen);
                        }
                    } break;
                default:
                    throw new Exception("Unknown field type " + field.Type);
            }
            #endregion
        }
예제 #2
0
        public void ReadField(ref StringBuilder content, Field field)
        {
            this.content = content;

            this.ReadType(field);
        }
예제 #3
0
        private void ReadType(Field field, bool read = true)
        {
            var count = field.Size;
            if (count == 0)
                count = GetValueByFiedName(field.SizeLink);

            switch (field.Type)
            {
                case DataType.Byte:    SetVal(field, read ? base.ReadByte(count)    : 0 ); break;
                case DataType.SByte:   SetVal(field, read ? base.ReadSByte(count)   : 0 ); break;
                case DataType.Short:   SetVal(field, read ? base.ReadInt16(count)   : 0 ); break;
                case DataType.Ushort:  SetVal(field, read ? base.ReadUInt16(count)  : 0 ); break;
                case DataType.Int:     SetVal(field, read ? base.ReadInt32(count)   : 0 ); break;
                case DataType.Uint:    SetVal(field, read ? base.ReadUInt32(count)  : 0 ); break;
                case DataType.Long:    SetVal(field, read ? base.ReadInt64(count)   : 0 ); break;
                case DataType.Ulong:   SetVal(field, read ? base.ReadUInt64(count)  : 0 ); break;
                case DataType.Float:   SetVal(field, read ? base.ReadFloat()        : 0f); break;
                case DataType.Double:  SetVal(field, read ? base.ReadDouble()       : 0d); break;

                case DataType.Pstring: SetVal(field, read ? base.ReadPString(count) : null, true); break;
                case DataType.String2: SetVal(field, read ? base.ReadString3(count) : null, true); break;
                case DataType.String:
                    {
                        if (stringTable != null) // adb db2
                        {
                            var offset = base.ReadInt32();
                            SetVal(field, stringTable[offset], true);
                        }
                        else if (read)
                        {
                            if (count == 0 && field.SizeLink == null)
                                SetVal(field, base.ReadCString(), true);
                            else
                                SetVal(field, base.ReadString2(count), true);
                        }
                        else
                        {
                            SetVal(field, null);
                        }
                    } break;
                case DataType.List:
                    {
                        var size = 0;
                        if (field.Size > 0)
                        {
                            size = read ? base.ReadSize(field.Size) : 0;
                            SetVal(field, size);
                        }
                        else if (!string.IsNullOrWhiteSpace(field.SizeLink))
                        {
                            size = GetValueByFiedName(field.SizeLink);
                        }
                        else if (field.Maxsize > 0)
                        {
                            size = field.Maxsize;
                        }

                        if (size > field.Maxsize)
                        {
                            throw new Exception(string.Format("Field <{0}>'{1}' size '{2}' is great then maxsize '{3}'!",
                                field.Type, field.Name, size, field.Maxsize));
                        }

                        for (int i = 0; i < field.Maxsize; ++i)
                        {
                            read = i < size;
                            foreach (var subfield in field.Fields)
                            {
                                ReadType(subfield, read);
                            }
                        }
                    } break;
                case DataType.StringList:
                    {
                        var dic = new Dictionary<string, int>();
                        foreach (var subField in field.Fields)
                        {
                            if (string.IsNullOrWhiteSpace(subField.Name))
                                throw new NullReferenceException("StringList: field name is empty!");

                            if (subField.Size == 0)
                                throw new NullReferenceException("StringList: field <" + subField.Name + "> has a size of zero!");

                            if (subField.Type != DataType.String && subField.Type != DataType.String2)
                                throw new NotImplementedException("StringList: field <" + subField.Name + "|" + subField.Type + "> is not supported!");

                            dic[subField.Name] = (int)mReadUInt32(subField.Size);
                        }

                        foreach (var subField in field.Fields)
                        {
                            var len = dic[subField.Name];
                            SetVal(subField, read ? base.ReadString2(len) : null, true);
                        }

                        dic.Clear();
                    } break;
                default:
                    break;
            }
        }
예제 #4
0
        private void SetVal(Field field, IConvertible value, bool isString = false)
        {
            if (!string.IsNullOrWhiteSpace(field.Name))
                this.valList[field.Name] = value;

            if (value == null)
                content.Append(", NULL");
            else if (!isString)
                content.Append(", " + value.ToString(CultureInfo.InvariantCulture));
            else
            {
                var val = value.ToString(CultureInfo.InvariantCulture);
                if (string.IsNullOrWhiteSpace(val))
                    content.Append(", NULL");
                else
                    content.Append(", \"" + val.Replace(@"'", @"\'").Replace("\"", "\\\"") + "\"");
            }

            if (Program.DebugOutput)
                content.Append(" /* " + field.Name + " */");
        }