Unified Riccati theory for optimal permanent and sampled-data control problems in finite and infinite time horizons