public static async Task <int?> UpdateAsync(this DirectDatabaseBase db, DirectModel model)
        {
            if (model.IntegerPrimary && !model.ID.HasValue)
            {
                throw new Exception("ID is not set, maybe this table was not loaded");
            }

            // UPDATE MobilePaywall.core.A SET A=1 WHERE AID=1
            string command = string.Format(DirectModelHelper.GetDatabase(model).QueryConstructUpdateQuery,
                                           model.GetTableName(),
                                           model.Snapshot.GetUpdateData(),
                                           model.GetIdNameValue(),
                                           (model.IntegerPrimary ? model.ID.Value.ToString() : string.Format("'{0}'", model.GetStringID())));

            DirectExecuteResult result = await db.ExecuteAsync(command);

            if (!result.IsSuccessfull)
            {
                return(null);
            }
            else
            {
                model.Snapshot.SetSnapshot();
                return(result.NumberOfRowsAffected);
            }
        }
Example #2
0
        internal static string ConstructUpdateUpdatedQuery(this DirectModel model)
        {
            if (model.IntegerPrimary && !model.ID.HasValue)
            {
                throw new Exception("ID is not set, maybe this table was not loaded");
            }

            model.OnBeforeUpdate();

            // UPDATE MobilePaywall.core.A SET A=1 WHERE AID=1

            string updatedPropName = string.Empty;

            foreach (var prop in model.Snapshot.PropertySignatures)
            {
                if (prop.Name.ToLower().Equals("updated"))
                {
                    updatedPropName = prop.Name;
                }
            }

            if (string.IsNullOrEmpty(updatedPropName))
            {
                return(string.Empty);
            }

            string command = string.Format(DirectModelHelper.GetDatabase(model).QueryConstructUpdateUpdatedQuery,
                                           model.GetTableName(),
                                           updatedPropName, model.GetDatabase().ConstructDateTimeParam(DateTime.Now),
                                           model.GetIdNameValue(),
                                           (model.IntegerPrimary ? model.ID.Value.ToString() : string.Format("'{0}'", model.GetStringID())));

            return(command);
        }
Example #3
0
        internal static string ConstructUpdateQuery(this DirectModel model)
        {
            if (model.IntegerPrimary && !model.ID.HasValue)
            {
                throw new Exception("ID is not set, maybe this table was not loaded");
            }

            model.OnBeforeUpdate();

            string updateData = model.Snapshot.GetUpdateData();

            if (string.IsNullOrEmpty(updateData))
            {
                return(string.Empty);
            }

            // UPDATE MobilePaywall.core.A SET A=1 WHERE AID=1
            string command = string.Format(DirectModelHelper.GetDatabase(model).QueryConstructUpdateQuery,
                                           model.GetTableName(),
                                           model.Snapshot.GetUpdateData(),
                                           model.GetIdNameValue(),
                                           (model.IntegerPrimary ? model.ID.Value.ToString() : string.Format("'{0}'", model.GetStringID())));

            return(command);
        }
Example #4
0
        internal static string ConstructInsertQuery(this DirectModel model)
        {
            model.OnBeforeInsert();
            string command = string.Format(DirectModelHelper.GetDatabase(model).QueryConstructInsertQuery,
                                           model.GetTableName(),
                                           model.Snapshot.GetPropertyNamesForInsert(), model.Snapshot.GetPropertyValuesForInsert());

            return(command);
        }
        public static async Task <T> InsertAsync <T>(this DirectDatabaseBase db, DirectModel model) where T : DirectModel
        {
            string command = string.Format(DirectModelHelper.GetDatabase(model).QueryConstructInsertQuery,
                                           model.GetTableName(),
                                           model.Snapshot.GetPropertyNamesForInsert(), model.Snapshot.GetPropertyValuesForInsert());
            DirectExecuteResult result = await db.ExecuteAsync(command);

            if (result.IsSuccessfull && result.LastID.HasValue)
            {
                model.ID = (int)result.LastID;
                model.Snapshot.SetSnapshot();
                return((T)model);
            }
            return((T)model);
        }
        public static async Task <bool> DeleteAsync(this DirectDatabaseBase db, DirectModel model)
        {
            if (model.IntegerPrimary && !model.ID.HasValue)
            {
                throw new Exception("THIS model has not ID");
            }

            string command = string.Format(DirectModelHelper.GetDatabase(model).QueryDelete,
                                           model.GetTableName(),
                                           model.GetIdNameValue(),
                                           (model.IntegerPrimary ? model.ID.Value.ToString() : string.Format("'{0}'", model.GetStringID())));
            DirectExecuteResult result = await db.ExecuteAsync(command);

            if (result.IsSuccessfull)
            {
                model.ID = null;
                model.Snapshot.SetSnapshot();
                return(true);
            }
            return(false);
        }