Functional equation $g(x+y) + g(x)g(y) = g(xy) + g(x) + g(y)$ - showing $g(x)=x$ for rational $x$ 
Find all functions $g: \mathbb R \to \mathbb R$ such that
$$g(x+y) + g(x)g(y) = g(xy) + g(x) + g(y) \text.$$

I have shown that $g(x) = 0$ for all $x$ and $g(x) = 2$ for all $x$ are solutions.
I have also show that $g(x) = x$ for all integer values of $x$ and am currently trying to strengthen this to all rational $x$.
Can someone give me a detailed solution showing that it holds for rational $x$ (I don't need it to be shown for real $x$ as well)?
 A: Denote$$P(x, y) : g(x + y) + g(x)g(y) = g(xy) + g(x) + g(y).$$Then$$P(x, y + z) : g(x + y + z) = g(x) + g(y + z) + g(xy + xz) - g(x)g(y + z) = g(x) + g(y) + g(z) + g(xy) + g(yz) + g(zx) + g(x)g(y)g(z) - g(x)g(y) - g(y)g(z) - g(z)g(x) + g(x^2yz) - g(xy)g(xz) - g(x)g(yz).$$From $P(y, x + z)$, $P(x, y + z)$, we get$$g(x^2yz) - g(xy)g(xz) - g(x)g(yz) = g(xy^2z) - g(xy)g(yz) - g(y)g(xz).$$Put $y = 1$, and we get$$g(x^2z) = (a - 1)g(xz) + g(x)g(xz),$$with$$a = 2 - g(1).$$Call this $(*)$. We have that $P(x, xz)$ and $(*)$ gives$$g(x + xz) = ag(xz) + g(x).$$Putting $z = 0$, we get $ag(0) = 0$, now if $a = 0$, then setting $x = 1$ gives$$g(z + 1) = g(1) = 2$$for all $z$. So $g(0) = 0$, now by setting $z = -1$, we have$$g(x) = -ag(-x),$$replacing $x$ by $-x$, we get$$g(x) = a^2g(x).$$Now, it is very easy to see $a$ can not be $-1$. So if $a = 1$, then we have$$g(x + xz) = g(x) + g(xz).$$Putting $xz = y$, we get$$g(x + y) = g(x) + g(y),$$and now $P(x, y)$ implies$$g(xy) = g(x)g(y),$$which is the well-known Cauchy equation, and hence$$g(x) = x$$for all $x$. So now if $a^2$ is not $1$, then$$g(x) = 2$$for all $x$.
