Пример #1
0
        private void _DbClean(Database db, string editorTable, string fieldName)
        {
            if (_dbTable == null || _dbCleanCallback == null)
            {
                return;
            }

            if (_dbCleanCallback != null)
            {
                fieldName = _dbCleanTableField;
            }

            string table;
            string field;
            var    a = fieldName.Split('.');

            switch (a.Count())
            {
            case 1:
                table = editorTable;
                field = a[0];
                break;

            case 2:
                table = a[0];
                field = a[1];
                break;

            default:
                table = a[1];
                field = a[2];
                break;
            }

            // Get the infromation from the database about the orphaned children
            var q = db.Query("select")
                    .Table(_dbTable)
                    .Get(_dbPKey);

            foreach (var pair in _dbFields)
            {
                var column = pair.Key;
                var prop   = pair.Value;

                if (prop is DbType && (DbType)prop != DbType.Content)
                {
                    q.Get(column);
                }
            }

            q.Where(_dbPKey, "(SELECT " + field + " FROM " + table + " WHERE " + field + " IS NOT NULL)", "NOT IN", false);

            var data = q.Exec().FetchAll();

            if (!data.Any())
            {
                return;
            }

            // Delete the selected rows, iff the develoeprs says to do so with a
            // true return.
            if (_dbCleanCallback(data) != true)
            {
                return;
            }

            var qDelete = db.Query("delete")
                          .Table(_dbTable);

            foreach (var row in data)
            {
                qDelete.OrWhere(_dbPKey, row[_dbPKey].ToString());
            }

            qDelete.Exec();
        }