Ejemplo n.º 1
0
 /// <summary>
 /// Inserts all the elements and all the relationships that are annotated with <c>CascadeOperation.CascadeInsert</c>
 /// into the database. If any element already exists in the database a 'Constraint' exception will be raised.
 /// Elements with a primary key that it's not <c>AutoIncrement</c> will need a valid identifier before calling
 /// this method.
 /// If the <c>recursive</c> flag is set to true, all the relationships annotated with
 /// <c>CascadeOperation.CascadeInsert</c> are inserted recursively in the database. This method will handle
 /// loops and inverse relationships correctly. <c>ReadOnly</c> properties will be omitted.
 /// </summary>
 /// <param name="conn">SQLite Net connection object</param>
 /// <param name="elements">Objects to be inserted.</param>
 /// <param name="recursive">If set to <c>true</c> all the insert-cascade properties will be inserted</param>
 public static Task InsertAllWithChildrenAsync(this SQLiteAsyncConnection conn, IEnumerable elements, bool recursive = false)
 {
     return(Task.Run(() =>
     {
         var connectionWithLock = SqliteAsyncConnectionWrapper.Lock(conn);
         using (connectionWithLock.Lock())
         {
             connectionWithLock.InsertAllWithChildren(elements, recursive);
         }
     }));
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Inserts or replace the element and all the relationships that are annotated with
 /// <c>CascadeOperation.CascadeInsert</c> into the database. If any element already exists in the database
 /// it will be replaced. Elements with <c>AutoIncrement</c> primary keys that haven't been assigned will
 /// be always inserted instead of replaced.
 /// If the <c>recursive</c> flag is set to true, all the relationships annotated with
 /// <c>CascadeOperation.CascadeInsert</c> are inserted recursively in the database. This method will handle
 /// loops and inverse relationships correctly. <c>ReadOnly</c> properties will be omitted.
 /// </summary>
 /// <param name="conn">SQLite Net connection object</param>
 /// <param name="element">Object to be inserted.</param>
 /// <param name="recursive">If set to <c>true</c> all the insert-cascade properties will be inserted</param>
 public static Task InsertOrReplaceWithChildrenAsync(this SQLiteAsyncConnection conn, object element, bool recursive = false)
 {
     return(Task.Run(() =>
     {
         var connectionWithLock = SqliteAsyncConnectionWrapper.Lock(conn);
         using (connectionWithLock.Lock())
         {
             connectionWithLock.InsertOrReplaceWithChildren(element, recursive);
         }
     }));
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Updates the with foreign keys of the current object and save changes to the database and
 /// updates the inverse foreign keys of the defined relationships so the relationships are
 /// stored correctly in the database. This operation will create or delete the required intermediate
 /// objects for ManyToMany relationships. All related objects must have a primary key assigned in order
 /// to work correctly. This also implies that any object with 'AutoIncrement' primary key must've been
 /// inserted in the database previous to this call.
 /// This method will also update inverse relationships of objects that currently exist in the object tree,
 /// but it won't update inverse relationships of objects that are not reachable through this object
 /// properties. For example, objects removed from a 'ToMany' relationship won't be updated in memory.
 /// </summary>
 /// <param name="conn">SQLite Net connection object</param>
 /// <param name="element">Object to be updated. Must already have been inserted in the database</param>
 public static Task UpdateWithChildrenAsync(this SQLiteAsyncConnection conn, object element)
 {
     return(Task.Run(() =>
     {
         var connectionWithLock = SqliteAsyncConnectionWrapper.Lock(conn);
         using (connectionWithLock.Lock())
         {
             connectionWithLock.UpdateWithChildren(element);
         }
     }));
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Deletes all the objects passed with IDs equal to the passed parameters from the database.
 /// Relationships are not taken into account in this method
 /// </summary>
 /// <param name="conn">SQLite Net connection object</param>
 /// <param name="primaryKeyValues">Primary keys of the objects to be deleted from the database</param>
 /// <typeparam name="T">The Entity type, it should match de database entity type</typeparam>
 public static Task DeleteAllIdsAsync <T>(this SQLiteAsyncConnection conn, IEnumerable <object> primaryKeyValues)
 {
     return(Task.Run(() =>
     {
         var connectionWithLock = SqliteAsyncConnectionWrapper.Lock(conn);
         using (connectionWithLock.Lock())
         {
             connectionWithLock.DeleteAllIds <T>(primaryKeyValues);
         }
     }));
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Deletes all the objects passed as parameters from the database.
 /// If recursive flag is set to true, all relationships marked with 'CascadeDelete' will be
 /// deleted from the database recursively. Inverse relationships and closed entity loops are handled
 /// correctly to avoid endless loops
 /// </summary>
 /// <param name="conn">SQLite Net connection object</param>
 /// <param name="recursive">If set to <c>true</c> all relationships marked with 'CascadeDelete' will be
 /// deleted from the database recursively</param>
 /// <param name="element">Object to be deleted from the database</param>
 public static Task DeleteAsync(this SQLiteAsyncConnection conn, object element, bool recursive)
 {
     return(Task.Run(() =>
     {
         var connectionWithLock = SqliteAsyncConnectionWrapper.Lock(conn);
         using (connectionWithLock.Lock())
         {
             connectionWithLock.Delete(element, recursive);
         }
     }));
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Deletes all the objects passed as parameters from the database.
 /// If recursive flag is set to true, all relationships marked with 'CascadeDelete' will be
 /// deleted from the database recursively. Inverse relationships and closed entity loops are handled
 /// correctly to avoid endless loops
 /// </summary>
 /// <param name="conn">SQLite Net connection object</param>
 /// <param name="recursive">If set to <c>true</c> all relationships marked with 'CascadeDelete' will be
 /// deleted from the database recursively</param>
 /// <param name="objects">Objects to be deleted from the database</param>
 public static Task DeleteAllAsync(this SQLiteAsyncConnection conn, IEnumerable objects, bool recursive = false)
 {
     return(Task.Run(() =>
     {
         var connectionWithLock = SqliteAsyncConnectionWrapper.Lock(conn);
         using (connectionWithLock.Lock())
         {
             connectionWithLock.DeleteAll(objects, recursive);
         }
     }));
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Fetches a specific property of the current object and keeps fetching recursively if the
        /// <c>recursive</c> flag has been set.
        /// </summary>
        /// <param name="conn">SQLite Net connection object</param>
        /// <param name="element">Element used to load all the relationship properties</param>
        /// <param name="relationshipProperty">Property to load from the database</param>
        /// <param name="recursive">If set to <c>true</c> all the relationships with
        /// <c>CascadeOperation.CascadeRead</c> will be loaded recusively.</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <typeparam name="T">Entity type where the object should be fetched from</typeparam>
        public static Task GetChildAsync <T>(this SQLiteAsyncConnection conn, T element, PropertyInfo relationshipProperty, bool recursive = false, CancellationToken cancellationToken = default(CancellationToken))
        {
            return(Task.Run(() =>
            {
                cancellationToken.ThrowIfCancellationRequested();

                var connectionWithLock = SqliteAsyncConnectionWrapper.Lock(conn);
                using (connectionWithLock.Lock())
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    connectionWithLock.GetChild(element, relationshipProperty, recursive);
                }
            }, cancellationToken));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Obtains the object from the database and fetches all the properties annotated with
        /// any subclass of <c>RelationshipAttribute</c>. If the object with the specified primary key doesn't
        /// exist in the database, an exception will be raised.
        /// </summary>
        /// <returns>The object with all the children loaded</returns>
        /// <param name="conn">SQLite Net connection object</param>
        /// <param name="pk">Primary key for the object to search in the database</param>
        /// <param name="recursive">If set to <c>true</c> all the relationships with
        /// <c>CascadeOperation.CascadeRead</c> will be loaded recusively.</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <typeparam name="T">Entity type where the object should be fetched from</typeparam>
        public static Task <T> GetWithChildrenAsync <T>(this SQLiteAsyncConnection conn, object pk, bool recursive = false, CancellationToken cancellationToken = default(CancellationToken))
            where T : class, new()
        {
            return(Task.Run(() =>
            {
                cancellationToken.ThrowIfCancellationRequested();

                var connectionWithLock = SqliteAsyncConnectionWrapper.Lock(conn);
                using (connectionWithLock.Lock())
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    return connectionWithLock.GetWithChildren <T>(pk, recursive);
                }
            }, cancellationToken));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Fetches all the entities of the specified type with the filter and fetches all the relationship
        /// properties of all the returned elements.
        /// </summary>
        /// <returns>List of all the elements of the type T that matches the filter with the children already loaded</returns>
        /// <param name="conn">SQLite Net connection object</param>
        /// <param name="filter">Filter that will be passed to the <c>Where</c> clause when fetching
        /// objects from the database. No relationship properties are allowed in this filter as they
        /// are loaded afterwards</param>
        /// <param name="recursive">If set to <c>true</c> all the relationships with
        /// <c>CascadeOperation.CascadeRead</c> will be loaded recusively.</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <typeparam name="T">Entity type where the object should be fetched from</typeparam>
        public static Task <List <T> > GetAllWithChildrenAsync <T>(this SQLiteAsyncConnection conn, Expression <Func <T, bool> > filter = null, bool recursive = false, CancellationToken cancellationToken = default(CancellationToken))
            where T : class, new()
        {
            return(Task.Run(() =>
            {
                cancellationToken.ThrowIfCancellationRequested();

                var connectionWithLock = SqliteAsyncConnectionWrapper.Lock(conn);
                using (connectionWithLock.Lock())
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    return connectionWithLock.GetAllWithChildren(filter, recursive);
                }
            }, cancellationToken));
        }
Ejemplo n.º 10
0
        public static Task <List <T> > GetAllWithChildrenAsync <T>(this SQLiteAsyncConnection conn,
                                                                   Expression <Func <T, bool> > filter      = null,
                                                                   Expression <Func <T, object> > orderExpr = null,
                                                                   int?limit     = null,
                                                                   int?offset    = null,
                                                                   int recursive = 1,
                                                                   CancellationToken cancellationToken = default(CancellationToken))
            where T : class, new()
        {
            return(Task.Run(() =>
            {
                cancellationToken.ThrowIfCancellationRequested();

                var connectionWithLock = SqliteAsyncConnectionWrapper.Lock(conn);
                using (connectionWithLock.Lock())
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    var result = connectionWithLock.GetAllWithChildren(filter, orderExpr, limit, offset, recursive);

                    return result;
                }
            }, cancellationToken));
        }