Fixed #16649 -- Refactored save_base logic

When saving an instance that came from the same DB we are saving into
use UPDATE - if not updated - INSERT, instead of SELECT - if found
UPDATE - else INSERT. This should save a query in majority of cases.
For example:
  a = SomeModel.objects.get(pk=pkval)
  # change a's fields
  a.save()
previously a.save() would have done a SELECT - UPDATE. The SELECT
is redundant as we know from model._state that we just fetched the
model from the DB. In the worst case the update doesn't touch any
rows and we will still correctly do an INSERT.

Also fixed #17341 -- made sure .save() commits transactions only after
the whole model has been saved. This wasn't the case in model
inheritance situations.

The save_base implementation was refactored into multiple methods.
A typical chain for inherited save is:
save_base()
    _save_parents(self)
        for each parent:
            _save_parents(parent)
            _save_table(parent)
    _save_table(self)