public override async Task <DirectExecuteResult> ExecuteAsync(string command) { command = this.OnBeforeCommandOverride(command); command = this.ConstructDatabaseNameAndScheme(command); DirectExecuteResult result = new DirectExecuteResult(); using (var sqlConnection = new MySqlConnection(this.ConnectionString)) { try { using (var sqlCommand = new MySqlCommand(command, sqlConnection)) { await sqlConnection.OpenAsync(); result.NumberOfRowsAffected = await sqlCommand.ExecuteNonQueryAsync(); result.LastID = sqlCommand.LastInsertedId; } } catch (Exception e) { result.Exception = e; this.OnException(DirectDatabaseExceptionType.OnExecuteAsync, command, e); } finally { await sqlConnection.CloseAsync(); } } return(result); }
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); } }
public static T Insert <T>(this DirectDatabaseBase db, DirectModel model) where T : DirectModel { DirectExecuteResult result = db.Execute(model.ConstructInsertQuery()); if (result.IsSuccessfull && result.LastID.HasValue) { model.ID = (int)result.LastID; model.Snapshot.SetSnapshot(); return((T)model); } return((T)model); }
public override async Task <DirectExecuteResult> ExecuteAsync(string command) { command = this.OnBeforeCommandOverride(command); command = this.ConstructDatabaseNameAndScheme(command); DirectExecuteResult result = new DirectExecuteResult(); using (var sqlConnection = new SqlConnection(this.ConnectionString)) { try { using (var sqlCommand = new SqlCommand(command, sqlConnection)) { await sqlConnection.OpenAsync(); result.NumberOfRowsAffected = await sqlCommand.ExecuteNonQueryAsync(); #region # get id of inserted object # if (command.ToLower().Contains("insert into ")) { sqlCommand.CommandText = "SELECT SCOPE_IDENTITY()"; SqlDataAdapter adapter = new SqlDataAdapter(sqlCommand); DataTable table = new DataTable(); adapter.Fill(table); string resultString = ""; if (table != null) { resultString = table.Rows[0][0].ToString(); } int insertedID; if (Int32.TryParse(resultString, out insertedID)) { result.LastID = insertedID; } } #endregion } } catch (Exception e) { result.Exception = e; this.OnException(DirectDatabaseExceptionType.OnExecuteAsync, command, e); } finally { sqlConnection.Close(); } } return(result); }
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 int?Update(this DirectDatabaseBase db, DirectModel model) { if (model.IntegerPrimary && !model.ID.HasValue) { throw new Exception("ID is not set, maybe this table was not loaded"); } DirectExecuteResult result = db.Execute(model.ConstructUpdateQuery()); if (!result.IsSuccessfull) { return(null); } else { model.Snapshot.SetSnapshot(); return(result.NumberOfRowsAffected); } }
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); }
public override DirectExecuteResult Execute(string command) { command = this.OnBeforeCommandOverride(command); command = this.ConstructDatabaseNameAndScheme(command); DirectExecuteResult result = new DirectExecuteResult(); bool hasException = false; using (var sqlConnection = new MySqlConnection(this.ConnectionString)) { try { using (var sqlCommand = new MySqlCommand(command, sqlConnection)) { sqlConnection.Open(); result.NumberOfRowsAffected = sqlCommand.ExecuteNonQuery(); result.LastID = sqlCommand.LastInsertedId; } } catch (Exception e) { result.Exception = e; this.OnException(DirectDatabaseExceptionType.OnExecute, command, e); } finally { sqlConnection.Close(); } // TODO: remove this part if (hasException) { System.Threading.Thread.Sleep(2500); Execute(command); } } return(result); }