示例#1
0
        public SqliteTable <T> WithoutMissingColumns(SqliteDatabase database)
        {
            var columns = new List <string>();

            using (var reader = database.Read($"PRAGMA table_info({TableName})"))
            {
                while (reader.TryRead())
                {
                    var column = reader.ColumnText(1);

                    if (column != null)
                    {
                        columns.Add(column);
                    }
                }
            }

            if (columns.Count < 1)
            {
                throw new MissingTableException($"Table '{TableName}' does not exist.");
            }

            var properties    = ImmutableArray.CreateRange(_properties.Where(p => columns.Contains(p.ColumnName)));
            var keyProperties = ImmutableArray.CreateRange(_keyProperties.Where(p => columns.Contains(p.ColumnName)));

            if (properties.IsDefaultOrEmpty)
            {
                throw new MissingTableException($"Table '{TableName}' has the wrong schema.");
            }

            return(new SqliteTable <T>(TableName, properties, keyProperties));
        }
示例#2
0
        public IEnumerable <KeyValuePair <long, T2> > ValuesWithRowId <T2>(
            SqliteDatabase database,
            Func <T2> generator,
            Action <QueryBuilder>?extendQuery = null) where T2 : T
        {
            var builder = new QueryBuilder()
                          .From($"`{TableName}`")
                          .Select(_properties.Select(p => $"`{p.ColumnName}`"))
                          .Select("`rowid`");

            extendQuery?.Invoke(builder);

            using (var reader = database.Read(builder.ToString()))
            {
                while (reader.TryRead())
                {
                    var item  = generator();
                    int index = 0;
                    foreach (var property in _properties)
                    {
                        property.PropertyHandler.LoadRecord(reader, index++, item);
                    }

                    long rowId = reader.ColumnInt64(index++);
                    yield return(KeyValuePair.Create(rowId, item));
                }
            }
        }
示例#3
0
        public IEnumerable <T2> Values <T2>(
            SqliteDatabase database,
            Func <T2> generator,
            Action <QueryBuilder>?extendQuery = null) where T2 : T
        {
            var builder = new QueryBuilder()
                          .From($"`{TableName}`")
                          .Select(_properties.Select(p => $"`{p.ColumnName}`"));

            extendQuery?.Invoke(builder);

            using (var reader = database.Read(builder.ToString()))
            {
                while (reader.TryRead())
                {
                    var item  = generator();
                    int index = 0;
                    foreach (var property in _properties)
                    {
                        property.PropertyHandler.LoadRecord(reader, index++, item);
                    }
                    yield return(item);
                }
            }
        }
示例#4
0
        public void AddMissingColumns(SqliteDatabase database)
        {
            CreateTable(database);

            var currentFields = new HashSet <string>();

            using (var reader = database.Read($"PRAGMA table_info({TableName})"))
            {
                while (reader.TryRead())
                {
                    var column = reader.ColumnText(1);

                    if (column != null)
                    {
                        currentFields.Add(column);
                    }
                }
            }

            var missingFields = new List <SqliteProperty <T> >(_properties);

            missingFields.RemoveAll(p => currentFields.Contains(p.ColumnName));

            var builder = new StringBuilder();

            foreach (var field in missingFields)
            {
                builder.Length = 0;
                builder
                .Append("ALTER TABLE ")
                .AppendField(TableName)
                .Append(" ADD COLUMN ")
                .AppendProperty(field);

                database.Execute(builder.ToString());
            }
        }