Esempio n. 1
0
        public IEnumerable <KeyValuePair <long, T2> > ValuesWithRowId <T2>(
            SqliteDatabase database,
            Func <T2> generator,
            Action <QueryBuilder>?extendQuery = null) where T2 : T
        {
            var builder = new QueryBuilder($"`{TableName}`")
                          .Select(p => $"`{p.ColumnName}`", _properties)
                          .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));
                }
            }
        }
Esempio n. 2
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));
        }
        public SqliteTableInsert(
            SqliteTable <T> table,
            SqliteDatabase database,
            ConflictResolution conflictResolution = ConflictResolution.Default)
        {
            _table = table;

            var sql = table.InsertSql(1, conflictResolution);

            _statement = database.Prepare(sql);
        }
Esempio n. 4
0
        public bool Exists(SqliteDatabase database)
        {
            string sql = "SELECT 1 FROM sqlite_master WHERE type='table' AND name=?";

            using (var statement = database.Prepare(sql))
            {
                statement.BindText(1, TableName);

                using (var reader = statement.GetReader())
                    return(reader.TryRead());
            }
        }
Esempio n. 5
0
        public void Insert(
            SqliteDatabase database,
            IEnumerable <T> records,
            ConflictResolution conflictResolution = ConflictResolution.Default)
        {
            int count        = 0;
            var recordBuffer = new T[database.VariableLimit / _properties.Length];
            var sql          = InsertSql(recordBuffer.Length, conflictResolution);
            var statement    = database.Prepare(sql);

            try
            {
                foreach (var record in records)
                {
                    recordBuffer[count++] = record;

                    if (count == recordBuffer.Length)
                    {
                        InsertAll();
                        count = 0;
                    }
                }

                if (count > 0)
                {
                    statement.Dispose();
                    sql       = InsertSql(count, conflictResolution);
                    statement = database.Prepare(sql);
                    InsertAll();
                }
            }
            finally
            {
                statement?.Dispose();
            }


            void InsertAll()
            {
                int n = 0;

                for (int i = 0; i < count; ++i)
                {
                    foreach (var property in _properties)
                    {
                        property.PropertyHandler.BindRecord(statement, ++n, recordBuffer[i]);
                    }
                }

                statement.Execute();
            }
        }
Esempio n. 6
0
        public void Insert(
            SqliteDatabase database,
            T record,
            ConflictResolution conflictResolution = ConflictResolution.Default)
        {
            var sql = InsertSql(1, conflictResolution);

            using (var statement = database.Prepare(sql))
            {
                for (int i = 0; i < _properties.Length; ++i)
                {
                    _properties[i].PropertyHandler.BindRecord(statement, i + 1, record);
                }

                statement.Execute();
            }
        }
Esempio n. 7
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());
            }
        }
Esempio n. 8
0
        public IEnumerable <T2> Values <T2>(
            SqliteDatabase database,
            Func <T2> generator,
            Action <QueryBuilder>?extendQuery = null) where T2 : T
        {
            var builder = new QueryBuilder($"`{TableName}`")
                          .Select(p => $"`{p.ColumnName}`", _properties);

            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);
                }
            }
        }
Esempio n. 9
0
        public void InsertWithTransaction(
            SqliteDatabase database,
            IEnumerable <T> records,
            ConflictResolution conflictResolution = ConflictResolution.Default)
        {
            using (var transaction = new SqliteTransaction(database))
            {
                var sql = InsertSql(1, conflictResolution);
                using (var statement = database.Prepare(sql))
                {
                    foreach (var record in records)
                    {
                        for (int i = 0; i < _properties.Length; ++i)
                        {
                            _properties[i].PropertyHandler.BindRecord(statement, i + 1, record);
                        }

                        statement.Execute();
                    }
                }

                transaction.Commit();
            }
        }
 public static IEnumerable <T> Values <T>(this SqliteTable <T> table, SqliteDatabase database) where T : class, new()
 {
     return(table.Values(database, () => new T()));
 }
 public static IEnumerable <KeyValuePair <long, T> > ValuesWithRowId <T>(this SqliteTable <T> table, SqliteDatabase database) where T : class, new()
 {
     return(table.ValuesWithRowId(database, () => new T()));
 }
Esempio n. 12
0
 public SqliteTransaction(SqliteDatabase database)
 {
     _database = database;
     _database.Execute("BEGIN TRANSACTION");
 }
Esempio n. 13
0
        public void CreateTable(SqliteDatabase database)
        {
            var sql = CreateTableSql();

            database.Execute(sql);
        }