Esempio n. 1
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 == '{');
        }
Esempio n. 2
0
        public string SelectAllSql()
        {
            TableMeta t = TableMeta.Get(typeof(T));
            TableMeta r = TableMeta.Get(typeof(R));

            return(Dialect.Actions.SelectAllJoinSql(new List <TableMeta>()
            {
                t, r
            }, new string[] { "t1", "t2" }));
        }
Esempio n. 3
0
        public static void ReflectEntities(Assembly asm)
        {
            foreach (var type in asm.GetTypes())
            {
                var attributes = type.GetCustomAttributes(typeof(TableAttribute), true);
                if (!attributes.Any())
                {
                    continue;
                }

                TableMeta.AddType(type);
            }
        }
Esempio n. 4
0
        public static AnonymousAdapter Open(string tableName)
        {
            TableMeta meta = TableMeta.Get(tableName);

            if (meta == null)
            {
                return(null);
            }

            return(new AnonymousAdapter(meta)
            {
                _tableName = meta.ParameterizedTableName
            });
        }
Esempio n. 5
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);
        }
Esempio n. 6
0
        public static IEnumerable <string> TableNames(bool excludeKnownTables)
        {
            using (SQLiteConnection conn = new SQLiteConnection(_connectionString))
                using (SQLiteCommand command = new SQLiteCommand("SELECT name FROM sqlite_master WHERE type='table' AND Name not like 'sqlite_%';", conn))
                {
                    BroadcastToListeners(command);

                    conn.Open();
                    var reader = command.ExecuteReader();

                    while (reader.Read())
                    {
                        if (!(excludeKnownTables && TableMeta.KnownTable(reader["name"] as string)))
                        {
                            yield return(reader["name"] as string);
                        }
                    }

                    reader.Dispose();
                }
        }
Esempio n. 7
0
        public Mash()
        {
            Table1 = Activator.CreateInstance <T>();
            TableMeta t = Get(typeof(T));

            Columns.AddRange(
                from tc in t.Columns
                select new TableColumn(tc)
            {
                Set = (tx, inst, val) => tc.Set(tx, ((Mash <T, R>)inst).Table1, val)
            });

            Table2 = Activator.CreateInstance <R>();
            TableMeta r = Get(typeof(R));

            Columns.AddRange(
                from tc in r.Columns
                select new TableColumn(tc)
            {
                Set = (tx, inst, val) => tc.Set(tx, ((Mash <T, R>)inst).Table2, val)
            });
        }
Esempio n. 8
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;
        }
Esempio n. 9
0
        private object ReadRow(IDataRecord reader, TableMeta meta)
        {
            object instance = Activator.CreateInstance <T>();

            for (int i = 0; i < meta.Columns.Count; i++)
            {
                if (Convert.IsDBNull(reader[i]))
                {
                    meta.Columns[i].SetValue(instance, null);
                }
                else if (typeof(Enum).IsAssignableFrom(meta.Columns[i].Type))
                {
                    meta.Columns[i].SetValue(instance, Enum.Parse(meta.Columns[i].Type, (string)reader[i]));
                }
                else
                {
                    meta.Columns[i].SetValue(instance, reader[i]);
                }
            }

            return(instance);
        }
Esempio n. 10
0
        public static IEnumerable <string> VerifySchema()
        {
            var tableNames = TableNames(false);

            foreach (var meta in TableMeta.GetAll())
            {
                if (meta.TableParamCount > 0)
                {
                    continue;
                }

                if (!tableNames.Contains(meta.ParameterizedTableName))
                {
                    (new AnonymousAdapter(meta)).CreateTable();
                    meta.CheckExists = false;
                    yield return("Create table: " + meta.ParameterizedTableName);
                }
                else
                {
                    yield return("Exists table: " + meta.ParameterizedTableName);
                }
            }
        }
Esempio n. 11
0
        public static TableAdapter <T> Open(params object[] args)
        {
            TableAdapter <T> adapter = Activator.CreateInstance <TableAdapter <T> >();

            if (typeof(IMash).IsAssignableFrom(typeof(T)))
            {
                adapter._isMultiTableJoin = true;
                return(adapter);
            }

            TableMeta meta = TableMeta.Get(typeof(T));

            if (meta is TableMetaInvalid)
            {
                throw new ArgumentException(typeof(T).FullName + " is invalid because " + string.Join(" and ", ((TableMetaInvalid)meta).Reasons.ToArray()));
            }

            if (args.Length != meta.TableParamCount)
            {
                throw new ArgumentException("Please specify " + meta.TableParamCount + " parameters");
            }

            string tableName = meta.CreateTableName(args);

            if (meta.CheckExists)
            {
                adapter.CreateTable(meta, tableName);
                // not a dynamicly created table, so don't need to check it exists now it's created
                meta.CheckExists = (meta.TableParamCount > 0);
            }

            adapter._meta      = meta;
            adapter._tableName = tableName;

            return(adapter);
        }
Esempio n. 12
0
 private void CreateTable(TableMeta tableMeta, string tableName)
 {
     ExecuteSql(Actions.CreateTable(tableMeta, tableName));
 }
Esempio n. 13
0
 public AnonymousAdapter(TableMeta meta)
 {
     _meta = meta;
 }
Esempio n. 14
0
 public Query(string sql, TableMeta meta)
 {
     _sql  = sql;
     _meta = meta;
 }
Esempio n. 15
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);
        }