Пример #1
0
        internal static void AddType(Type type )
        {
            TableMeta meta;

            if (TableMetaDictionaryType.TryGetValue(type, out meta))
            {
                meta.CheckExists = true;
                return;
            }

            meta = new TableMeta() { _tableType = type };

            GetTableName( meta, type );

            var errors = CreateColumnList( meta, type );
            if (errors.Any())
                meta = new TableMetaInvalid() {Reasons = errors.ToList()};

            if (meta.Columns.Count == 0 && !(meta is TableMetaInvalid))
                meta = new TableMetaInvalid();

            if (meta.Columns.Count == 0)
                ((TableMetaInvalid)meta).Reasons.Add("No columns defined");
            else
                meta.HasAutoIncrementPrimaryKey = meta.Columns.Any( col => col.PrimaryKey && col.AutoIncrement );

            DbConnection.BroadcastToListeners("Add new type: " + type.FullName);
            TableMetaDictionaryType.Add( type, meta );
        }
Пример #2
0
		public static TableMeta ToMeta(string tableName)
		{
            // Parse a string like;
            // CREATE TABLE SimpleTable ([Id] long not null,[Test] nvarchar(300) ,[When] date , PRIMARY KEY ([Id] ))
			
            TableMeta meta = new TableMeta {ParameterizedTableName = tableName};

			string schema = GetDbSchemaFor( tableName );
			if (string.IsNullOrEmpty( schema ))
				return null;

			StringParse parse = new StringParse( schema );
			if (!parse.Read("CREATE TABLE " + tableName + " ("))
				return null;
			
			while (!parse.Peek(")") && !parse.IsEmpty)
			{
                parse.SkipWhitespace();


				if (parse.Peek("[") || parse.Contains(' '))
				{
                    string fieldName;
                    if (parse.Peek("["))
                    {
                        fieldName = parse.ReadBetween('[', ']');
                    }
                    else
                    {
                        if (parse.Peek("PRIMARY KEY"))
                            break;

                        fieldName = parse.ReadTo(' ');                        
                    }

                    parse.SkipWhitespace();

					TableColumn tc = new TableColumn
					                 	{
					                 		Name = fieldName,
					                 		Get = (t, inst) => ((AnonmousTable) inst)[ t.RawName ],
					                 		Set = (t, inst, val) => ((AnonmousTable) inst)[ t.RawName ] = val
					                 	};

					parse.While( KeywordTriggerDefinitions, tc );

                    meta.Columns.Add(tc);
				}

                if (parse.Peek(","))
                {
                    parse.Read(",");
                    continue;
                }

                if (parse.Peek(")"))
                    break;

                if (parse.Peek("PRIMARY KEY"))
                    break;
             
  				throw new NotImplementedException("Incomplete parsing of " + tableName);
			}

			return meta;
		}
Пример #3
0
        private static void GetTableName( TableMeta meta, Type type )
        {
            var attributes = type.GetCustomAttributes( typeof( TableAttribute ), true );
            meta.ParameterizedTableName = string.IsNullOrEmpty( ((TableAttribute)attributes[0]).Name )
                                ? type.Name
                                : ((TableAttribute)attributes[0]).Name;

            meta.TableParamCount = meta.ParameterizedTableName.Count( c => c == '{' );
        }
Пример #4
0
        private static IEnumerable<string> CreateColumnList( TableMeta meta, Type type )
        {
            List<PropertyFieldInfo> members = new List<PropertyFieldInfo>();

            foreach (PropertyInfo pi in type.GetProperties( BindingFlags.Public | BindingFlags.Instance ))
                members.Add(new PropertyFieldInfo(pi));

            foreach (FieldInfo fi in type.GetFields( BindingFlags.Public | BindingFlags.Instance ))
                members.Add(new PropertyFieldInfo(fi));

            foreach (var member in members)
            {
                var definition = member.FieldAttribute();
                if (definition == null)
                    continue;

                if (!Column.IsSupported(member.Type))
                {
                    yield return "Unsupported type " + member.Type + " in field " + member.Name;
                    continue;
                }

                if (string.IsNullOrEmpty( definition.Name ))
                    definition.Name = member.Name;

                meta.Columns.Add(new TableColumn(definition, member));

            }

            yield break;
        }