/// <summary>
        /// Prepares a SQLite statement that can be bound to an object primary key to delete row from the database.
        /// </summary>
        /// <returns>A prepared statement.</returns>
        /// <param name="This">The database connection</param>
        /// <typeparam name="T">The mapped type.</typeparam>
        public static IStatement PrepareDeleteStatement <T>(this IDatabaseConnection This)
        {
            var tableMapping = TableMapping.Get <T>();
            var sql          = deleteQueries.GetValue(tableMapping, mapping =>
            {
                var primaryKeyColumn = mapping.PrimaryKeyColumn();
                return(SQLBuilder.DeleteUsingPrimaryKey(mapping.TableName, primaryKeyColumn));
            });


            return(This.PrepareStatement(sql));
        }
        /// <summary>
        /// Prepares a SQLite statement that can be bound to an object primary key to retrieve an instance from the database.
        /// </summary>
        /// <returns>A prepared statement.</returns>
        /// <param name="This">The database connection</param>
        /// <typeparam name="T">The mapped type.</typeparam>
        public static IStatement PrepareFindStatement <T>(this IDatabaseConnection This)
        {
            Contract.Requires(This != null);

            var tableMapping = TableMapping.Get <T>();
            var sql          = findQueries.GetValue(tableMapping, mapping =>
            {
                var column = mapping.PrimaryKeyColumn();
                return(SQLBuilder.SelectWhereColumnEquals(tableMapping.TableName, column));
            });

            return(This.PrepareStatement(sql));
        }
        private static IEnumerable <KeyValuePair <T, T> > YieldInsertOrReplaceAll <T>(
            this IDatabaseConnection This,
            IEnumerable <T> objects,
            Func <IReadOnlyList <ResultSetValue>, T> resultSelector)
        {
            Contract.Requires(objects != null);
            Contract.Requires(resultSelector != null);

            using (var insertOrReplaceStmt = This.PrepareInsertOrReplaceStatement <T>())
                using (var findStmt = This.PrepareFindByRowId(TableMapping.Get <T>().TableName))
                {
                    foreach (var obj in objects)
                    {
                        insertOrReplaceStmt.ExecuteWithProperties(obj);
                        var rowId = This.LastInsertedRowId;
                        yield return(new KeyValuePair <T, T>(obj, findStmt.Query(rowId).Select(resultSelector).First()));
                    }
                }
        }
        /// <summary>
        /// Creates or migrate a table in the database for the given table mapping, creating indexes if needed.
        /// </summary>
        /// <param name="This">The database connection.</param>
        /// <typeparam name="T">The mapped type.</typeparam>
        public static void InitTable <T>(this IDatabaseConnection This)
        {
            Contract.Requires(This != null);

            This.RunInTransaction(db =>
            {
                var tableMapping = TableMapping.Get <T>();
                db.CreateTableIfNotExists(tableMapping.TableName, CreateFlags.None, tableMapping.Columns);

                if (db.Changes != 0)
                {
                    db.MigrateTable(tableMapping);
                }

                foreach (var index in tableMapping.Indexes)
                {
                    db.CreateIndex(index.Key, tableMapping.TableName, index.Value.Columns, index.Value.Unique);
                }
            });
        }
Exemplo n.º 5
0
        /// <summary>
        /// Returns a function that convert a SQLite row into an instance of type T using the provided builder functions. The builder
        /// function may return the same instane of builder more than once, provided that the instance is thread local or locked such
        /// that only a single given thread ever has access to the instance between calls to builder and build.
        /// </summary>
        /// <returns>The result selector function.</returns>
        /// <param name="builder">A function that provides a builder object that can be used to build an instance of T.</param>
        /// <param name="build">A function that builds and instance of T using the builder.</param>
        /// <typeparam name="TBuilder">The builder type.</typeparam>
        /// <typeparam name="T">The mapped type.</typeparam>
        public static Func <IReadOnlyList <IResultSetValue>, T> RowToObject <TBuilder, T>(Func <TBuilder> builder, Func <TBuilder, T> build)
        {
            Contract.Requires(builder != null);
            Contract.Requires(build != null);

            var columns   = new Dictionary <string, PropertyInfo>();
            var tableName = TableMapping.Get <T>().TableName;

            var typ = typeof(TBuilder);

            var props = typ.GetNotIgnoredSettableProperties();

            foreach (var prop in props)
            {
                var columnName = prop.GetColumnName();
                columns.Add(columnName, prop);
            }

            return((IReadOnlyList <IResultSetValue> row) =>
            {
                var builderInst = builder();

                foreach (var column in row)
                {
                    var columnInfo = column.ColumnInfo;
                    PropertyInfo prop;
                    if (columnInfo.TableName == tableName && columns.TryGetValue(columnInfo.OriginName, out prop))
                    {
                        var value = column.ToObject(prop.PropertyType);
                        prop.SetValue(builderInst, value, null);
                    }
                }

                return build(builderInst);
            });
        }
        /// <summary>
        /// Deletes all rows in a given table.
        /// </summary>
        /// <param name="This">The database connection.</param>
        /// <typeparam name="T">The mapped type.</typeparam>
        public static void DeleteAllRows <T>(this IDatabaseConnection This)
        {
            var tableMapping = TableMapping.Get <T>();

            This.Execute(SQLBuilder.DeleteAll(tableMapping.TableName));
        }
        /// <summary>
        /// Drops the table if it exists. Otherwise this is a no-op.
        /// </summary>
        /// <param name="This">The database connection.</param>
        /// <typeparam name="T">The mapped type.</typeparam>
        /// <seealso href="https://www.sqlite.org/lang_droptable.html"/>
        public static void DropTableIfExists <T>(this IDatabaseConnection This)
        {
            var tableMapping = TableMapping.Get <T>();

            This.Execute(SQLBuilder.DropTableIfExists(tableMapping.TableName));
        }