public override bool DeleteMatching(Type type, Selector[] selectors) { var obj = GenericDatabaseEntity.GetNonDisposableRefenceObject(type); if (obj == null) return false; var result = obj.GetColumns(true); if (result == null || result.GetPrimaryKeys() == null || !result.GetPrimaryKeys().Any()) { throw new DatabaseException(DatabaseException.ErrorType.NoPrimaryKeyColumnsFound); } var connection = GenericConnectionManager.GetConnectionManager(GetSupportedDatabase()).GetConnection(type); var command = connection.CreateCommand(); var whereStatement = ""; if (selectors != null) whereStatement = selectors.Aggregate("", (current, selector) => current + ((current == "" ? "" : " AND ") + selector.SetToCommand(ref command))); var selectStatement = "DELETE FROM " + obj.GetType().Name + (selectors != null && selectors.Any() ? (" WHERE " + whereStatement) : ""); command.CommandText = selectStatement; var executionResult = command.ExecuteNonQuery(); command.Dispose(); if (executionResult >= 0) { _OnBulkDelete(type, selectors); return true; } return false; }
public override void FillDataTable(Type type, ref DataTable table, Selector[] selectors, int limit) { var obj = GenericDatabaseEntity.GetNonDisposableRefenceObject(type); if (obj == null) return; var commandString = DatabaseEntity.GetSelectCommandString(type); var whereQuery = ""; var command = GenericConnectionManager.GetConnectionManager(GetSupportedDatabase()).GetConnection(type).CreateCommand(); if (selectors != null && selectors.Any()) { whereQuery = selectors.Aggregate(whereQuery, (current, selector) => current + ((current == "" ? "" : " AND ") + selector.SetToCommand(ref command))); } command.CommandText = commandString + (whereQuery != "" ? (" WHERE " + whereQuery) : "") + (limit > 0 ? (" LIMIT " + limit) : ""); var reader = command.ExecuteReader(); var fieldInfos = obj.GetColumns(true).GetOtherColumns().Where(i => ((TableColumn)i.GetCustomAttributes(typeof(TableColumn), true)[0]).IsRetrievableFromDatabase).ToArray(); table.Rows.Clear(); table.Columns.Clear(); var translators = new List<string[]>(); foreach (var fieldInfo in fieldInfos) { var cInfo = ((TableColumn)fieldInfo.GetCustomAttributes(typeof(TableColumn), true)[0]); if (fieldInfo.FieldType == typeof(bool)) table.Columns.Add(cInfo.GridDisplayName ?? fieldInfo.Name, typeof(bool)); else table.Columns.Add(cInfo.GridDisplayName ?? fieldInfo.Name); translators.Add(cInfo.Translators); } if (!reader.Read()) { reader.Close(); command.Dispose(); return; } frmLoadingDialog.ShowWindow(); do { if (frmLoadingDialog.GetStatus()) break; var row = table.Rows.Add(); for (var i = 0; i < table.Columns.Count; i++) { try { if (translators[i] != null) { var index = (int)reader[i]; row[i] = (translators.Count > i && i >= 0) ? translators[i][index] : index.ToString(CultureInfo.InvariantCulture); } else row[i] = reader[i]; } catch { } } } while (reader.Read()); reader.Close(); frmLoadingDialog.HideWindow(); command.Dispose(); }
public override GenericDatabaseEntity[] Select(Type type, Selector[] selectors) { var obj = GenericDatabaseEntity.GetNonDisposableRefenceObject(type); if (obj == null) return new DatabaseEntity[0]; var result = obj.GetColumns(true); if (result == null || result.GetPrimaryKeys() == null || !result.GetPrimaryKeys().Any()) { throw new DatabaseException(DatabaseException.ErrorType.NoPrimaryKeyColumnsFound); } var selectFields = result.GetOtherColumns() .Where(i => ((TableColumn)i.GetCustomAttributes(typeof(TableColumn), true)[0]).IsRetrievableFromDatabase) .Aggregate("", (current, selector) => current + ((current == "" ? "" : ", ") + selector.Name)); var connection = GenericConnectionManager.GetConnectionManager(GetSupportedDatabase()).GetConnection(type); var command = connection.CreateCommand(); var whereStatement = ""; if (selectors != null) { whereStatement = selectors.Aggregate("", (current, selector) => current + ((current == "" ? "" : " AND ") + selector.SetToCommand(ref command))); } var selectStatement = "SELECT " + selectFields + " FROM " + obj.GetType().Name + (selectors != null && selectors.Any() ? (" WHERE " + whereStatement) : ""); command.CommandText = selectStatement; var reader = command.ExecuteReader(); var results = ParseDataReader(type, reader); command.Dispose(); return results; }