Ejemplo n.º 1
0
        internal static bool Save <T>(this IDbCommand dbCmd, T obj)
        {
            var id          = obj.GetId();
            var existingRow = dbCmd.SingleById <T>(id);

            if (Equals(existingRow, default(T)))
            {
                var modelDef = typeof(T).GetModelDefinition();
                if (modelDef.HasAutoIncrementId)
                {
                    var newId  = dbCmd.Insert(obj, selectIdentity: true);
                    var safeId = OrmLiteConfig.DialectProvider.ConvertDbValue(newId, modelDef.PrimaryKey.FieldType);
                    modelDef.PrimaryKey.SetValueFn(obj, safeId);
                }
                else
                {
                    if (OrmLiteConfig.InsertFilter != null)
                    {
                        OrmLiteConfig.InsertFilter(dbCmd, obj);
                    }

                    dbCmd.Insert(obj);
                }
                return(true);
            }

            if (OrmLiteConfig.UpdateFilter != null)
            {
                OrmLiteConfig.UpdateFilter(dbCmd, obj);
            }

            dbCmd.Update(obj);
            return(false);
        }
        public static void InsertOnly<T>(this IDbCommand dbCmd, T obj, SqlExpression<T> onlyFields)
        {
            if (OrmLiteConfig.InsertFilter != null)
                OrmLiteConfig.InsertFilter(dbCmd, obj);

            var sql = dbCmd.GetDialectProvider().ToInsertRowStatement(dbCmd, obj, onlyFields.InsertFields);
            dbCmd.ExecuteSql(sql);
        }
Ejemplo n.º 3
0
        internal static Task InsertOnlyAsync <T>(this IDbCommand dbCmd, T obj, string[] onlyFields, CancellationToken token)
        {
            if (OrmLiteConfig.InsertFilter != null)
            {
                OrmLiteConfig.InsertFilter(dbCmd, obj);
            }

            var sql = dbCmd.GetDialectProvider().ToInsertRowStatement(dbCmd, obj, onlyFields);

            return(dbCmd.ExecuteSqlAsync(sql, token));
        }
        internal static void InsertAll <T>(this IDbCommand dbCmd, IEnumerable <T> objs)
        {
            IDbTransaction dbTrans = null;

            try
            {
                if (dbCmd.Transaction == null)
                {
                    dbCmd.Transaction = dbTrans = dbCmd.Connection.BeginTransaction();
                }

                var dialectProvider = dbCmd.GetDialectProvider();

                dialectProvider.PrepareParameterizedInsertStatement <T>(dbCmd);

                foreach (var obj in objs)
                {
                    if (OrmLiteConfig.InsertFilter != null)
                    {
                        OrmLiteConfig.InsertFilter(dbCmd, obj);
                    }

                    dialectProvider.SetParameterValues <T>(dbCmd, obj);

                    try
                    {
                        dbCmd.ExecNonQuery();
                    }
                    catch (Exception ex)
                    {
                        Log.Error("SQL ERROR: {0}".Fmt(dbCmd.GetLastSqlAndParams()), ex);
                        throw;
                    }
                }

                if (dbTrans != null)
                {
                    dbTrans.Commit();
                }
            }
            finally
            {
                if (dbTrans != null)
                {
                    dbTrans.Dispose();
                }
            }
        }
Ejemplo n.º 5
0
        internal static Task <long> InsertAsync <T>(this IDbCommand dbCmd, T obj, bool selectIdentity, CancellationToken token)
        {
            if (OrmLiteConfig.InsertFilter != null)
            {
                OrmLiteConfig.InsertFilter(dbCmd, obj);
            }

            OrmLiteConfig.DialectProvider.PrepareParameterizedInsertStatement <T>(dbCmd);
            OrmLiteConfig.DialectProvider.SetParameterValues <T>(dbCmd, obj);

            if (selectIdentity)
            {
                return(OrmLiteConfig.DialectProvider.InsertAndGetLastInsertIdAsync <T>(dbCmd, token));
            }

            return(dbCmd.ExecNonQueryAsync(token).Then(i => (long)i));
        }
Ejemplo n.º 6
0
        internal static long Insert <T>(this IDbCommand dbCmd, T obj, bool selectIdentity = false)
        {
            if (OrmLiteConfig.InsertFilter != null)
            {
                OrmLiteConfig.InsertFilter(dbCmd, obj);
            }

            OrmLiteConfig.DialectProvider.PrepareParameterizedInsertStatement <T>(dbCmd);

            OrmLiteConfig.DialectProvider.SetParameterValues <T>(dbCmd, obj);

            if (selectIdentity)
            {
                return(OrmLiteConfig.DialectProvider.InsertAndGetLastInsertId <T>(dbCmd));
            }

            return(dbCmd.ExecNonQuery());
        }
Ejemplo n.º 7
0
        internal static Task InsertAllAsync <T>(this IDbCommand dbCmd, IEnumerable <T> objs, CancellationToken token)
        {
            IDbTransaction dbTrans = null;

            if (dbCmd.Transaction == null)
            {
                dbCmd.Transaction = dbTrans = dbCmd.Connection.BeginTransaction();
            }

            var dialectProvider = dbCmd.GetDialectProvider();

            dialectProvider.PrepareParameterizedInsertStatement <T>(dbCmd);

            return(objs.EachAsync((obj, i) =>
            {
                if (OrmLiteConfig.InsertFilter != null)
                {
                    OrmLiteConfig.InsertFilter(dbCmd, obj);
                }

                dialectProvider.SetParameterValues <T>(dbCmd, obj);

                return dbCmd.ExecNonQueryAsync(token);
            })
                   .ContinueWith(t =>
            {
                if (dbTrans != null && t.IsSuccess())
                {
                    dbTrans.Commit();
                }

                if (dbTrans != null)
                {
                    dbTrans.Dispose();
                }

                if (t.IsFaulted)
                {
                    throw t.Exception;
                }
            }));
        }
Ejemplo n.º 8
0
        public static Task <int> InsertOnlyAsync <T>(this IDbCommand dbCmd, Expression <Func <T> > insertFields, CancellationToken token)
        {
            if (insertFields == null)
            {
                throw new ArgumentNullException("insertFields");
            }

            if (OrmLiteConfig.InsertFilter != null)
            {
                OrmLiteConfig.InsertFilter(dbCmd, CachedExpressionCompiler.Evaluate(insertFields));
            }

            var insertFieldsValues = insertFields.AssignedValues();

            dbCmd.GetDialectProvider().PrepareParameterizedInsertStatement <T>(dbCmd, insertFieldsValues.Keys);

            dbCmd.SetParameters(insertFieldsValues, excludeDefaults: false);

            return(dbCmd.ExecNonQueryAsync(token));
        }
        internal static long Insert <T>(this IDbCommand dbCmd, T obj, bool selectIdentity = false)
        {
            if (OrmLiteConfig.InsertFilter != null)
            {
                OrmLiteConfig.InsertFilter(dbCmd, obj);
            }

            var dialectProvider = dbCmd.GetDialectProvider();

            dialectProvider.PrepareParameterizedInsertStatement <T>(dbCmd,
                                                                    insertFields: OrmLiteUtils.GetNonDefaultValueInsertFields(obj));

            dialectProvider.SetParameterValues <T>(dbCmd, obj);

            if (selectIdentity)
            {
                return(dialectProvider.InsertAndGetLastInsertId <T>(dbCmd));
            }

            return(dbCmd.ExecNonQuery());
        }
        internal static void InsertAll <T>(this IDbCommand dbCmd, IEnumerable <T> objs)
        {
            IDbTransaction dbTrans = null;

            try
            {
                if (dbCmd.Transaction == null)
                {
                    dbCmd.Transaction = dbTrans = dbCmd.Connection.BeginTransaction();
                }

                var dialectProvider = dbCmd.GetDialectProvider();

                dialectProvider.PrepareParameterizedInsertStatement <T>(dbCmd);

                foreach (var obj in objs)
                {
                    if (OrmLiteConfig.InsertFilter != null)
                    {
                        OrmLiteConfig.InsertFilter(dbCmd, obj);
                    }

                    dialectProvider.SetParameterValues <T>(dbCmd, obj);

                    dbCmd.ExecNonQuery();
                }

                if (dbTrans != null)
                {
                    dbTrans.Commit();
                }
            }
            finally
            {
                if (dbTrans != null)
                {
                    dbTrans.Dispose();
                }
            }
        }
Ejemplo n.º 11
0
        internal static async Task <int> SaveAllAsync <T>(this IDbCommand dbCmd, IEnumerable <T> objs, CancellationToken token)
        {
            var saveRows = objs.ToList();

            var firstRow = saveRows.FirstOrDefault();

            if (Equals(firstRow, default(T)))
            {
                return(0);
            }

            var modelDef = typeof(T).GetModelDefinition();

            var firstRowId     = modelDef.GetPrimaryKey(firstRow);
            var defaultIdValue = firstRowId != null?firstRowId.GetType().GetDefaultValue() : null;

            var idMap = defaultIdValue != null
                ? saveRows.Where(x => !defaultIdValue.Equals(modelDef.GetPrimaryKey(x))).ToSafeDictionary(x => modelDef.GetPrimaryKey(x))
                : saveRows.Where(x => modelDef.GetPrimaryKey(x) != null).ToSafeDictionary(x => modelDef.GetPrimaryKey(x));

            var existingRowsMap = (await dbCmd.SelectByIdsAsync <T>(idMap.Keys, token)).ToDictionary(x => modelDef.GetPrimaryKey(x));

            var rowsAdded = 0;

            IDbTransaction dbTrans = null;

            if (dbCmd.Transaction == null)
            {
                dbCmd.Transaction = dbTrans = dbCmd.Connection.BeginTransaction();
            }

            var dialectProvider = dbCmd.GetDialectProvider();

            try
            {
                foreach (var row in saveRows)
                {
                    var id = modelDef.GetPrimaryKey(row);
                    if (id != defaultIdValue && existingRowsMap.ContainsKey(id))
                    {
                        if (OrmLiteConfig.UpdateFilter != null)
                        {
                            OrmLiteConfig.UpdateFilter(dbCmd, row);
                        }

                        await dbCmd.UpdateAsync(row, token);
                    }
                    else
                    {
                        if (modelDef.HasAutoIncrementId)
                        {
                            var newId = await dbCmd.InsertAsync(row, selectIdentity : true, token : token);

                            var safeId = dialectProvider.ConvertDbValue(newId, modelDef.PrimaryKey.FieldType);
                            modelDef.PrimaryKey.SetValueFn(row, safeId);
                            id = newId;
                        }
                        else
                        {
                            if (OrmLiteConfig.InsertFilter != null)
                            {
                                OrmLiteConfig.InsertFilter(dbCmd, row);
                            }

                            await dbCmd.InsertAsync(token, row);
                        }

                        rowsAdded++;
                    }

                    if (modelDef.RowVersion != null)
                    {
                        modelDef.RowVersion.SetValueFn(row, await dbCmd.GetRowVersionAsync(modelDef, id, token));
                    }
                }

                if (dbTrans != null)
                {
                    dbTrans.Commit();
                }
            }
            finally
            {
                if (dbTrans != null)
                {
                    dbTrans.Dispose();
                }
            }

            return(rowsAdded);
        }
Ejemplo n.º 12
0
        internal static int SaveAll <T>(this IDbCommand dbCmd, IEnumerable <T> objs)
        {
            var saveRows = objs.ToList();

            var firstRow = saveRows.FirstOrDefault();

            if (Equals(firstRow, default(T)))
            {
                return(0);
            }

            var defaultIdValue = firstRow.GetId().GetType().GetDefaultValue();

            var idMap = defaultIdValue != null
                ? saveRows.Where(x => !defaultIdValue.Equals(x.GetId())).ToSafeDictionary(x => x.GetId())
                : saveRows.Where(x => x.GetId() != null).ToSafeDictionary(x => x.GetId());

            var existingRowsMap = dbCmd.SelectByIds <T>(idMap.Keys).ToDictionary(x => x.GetId());

            var modelDef = typeof(T).GetModelDefinition();

            var rowsAdded = 0;

            IDbTransaction dbTrans = null;

            if (dbCmd.Transaction == null)
            {
                dbCmd.Transaction = dbTrans = dbCmd.Connection.BeginTransaction();
            }

            try
            {
                foreach (var row in saveRows)
                {
                    var id = row.GetId();
                    if (id != defaultIdValue && existingRowsMap.ContainsKey(id))
                    {
                        if (OrmLiteConfig.UpdateFilter != null)
                        {
                            OrmLiteConfig.UpdateFilter(dbCmd, row);
                        }

                        dbCmd.Update(row);
                    }
                    else
                    {
                        if (modelDef.HasAutoIncrementId)
                        {
                            var newId  = dbCmd.Insert(row, selectIdentity: true);
                            var safeId = OrmLiteConfig.DialectProvider.ConvertDbValue(newId, modelDef.PrimaryKey.FieldType);
                            modelDef.PrimaryKey.SetValueFn(row, safeId);
                        }
                        else
                        {
                            if (OrmLiteConfig.InsertFilter != null)
                            {
                                OrmLiteConfig.InsertFilter(dbCmd, row);
                            }

                            dbCmd.Insert(row);
                        }

                        rowsAdded++;
                    }
                }

                if (dbTrans != null)
                {
                    dbTrans.Commit();
                }
            }
            finally
            {
                if (dbTrans != null)
                {
                    dbTrans.Dispose();
                }
            }

            return(rowsAdded);
        }