servers/
postgres.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15mod auth_handler;
16mod fixtures;
17mod handler;
18mod server;
19mod types;
20
21pub(crate) const METADATA_USER: &str = "user";
22pub(crate) const METADATA_DATABASE: &str = "database";
23/// key to store our parsed catalog
24pub(crate) const METADATA_CATALOG: &str = "catalog";
25/// key to store our parsed schema
26pub(crate) const METADATA_SCHEMA: &str = "schema";
27
28use std::collections::HashMap;
29use std::net::SocketAddr;
30use std::sync::Arc;
31
32use ::auth::UserProviderRef;
33use derive_builder::Builder;
34use pgwire::api::auth::ServerParameterProvider;
35use pgwire::api::copy::NoopCopyHandler;
36use pgwire::api::{ClientInfo, PgWireServerHandlers};
37pub use server::PostgresServer;
38use session::context::Channel;
39use session::Session;
40
41use self::auth_handler::PgLoginVerifier;
42use self::handler::DefaultQueryParser;
43use crate::query_handler::sql::ServerSqlQueryHandlerRef;
44
45pub(crate) struct GreptimeDBStartupParameters {
46    version: String,
47}
48
49impl GreptimeDBStartupParameters {
50    fn new() -> GreptimeDBStartupParameters {
51        GreptimeDBStartupParameters {
52            version: format!("16.3-greptimedb-{}", env!("CARGO_PKG_VERSION")),
53        }
54    }
55}
56
57impl ServerParameterProvider for GreptimeDBStartupParameters {
58    fn server_parameters<C>(&self, _client: &C) -> Option<HashMap<String, String>>
59    where
60        C: ClientInfo,
61    {
62        Some(HashMap::from([
63            ("server_version".to_owned(), self.version.clone()),
64            ("server_encoding".to_owned(), "UTF8".to_owned()),
65            ("client_encoding".to_owned(), "UTF8".to_owned()),
66            ("DateStyle".to_owned(), "ISO YMD".to_owned()),
67            ("integer_datetimes".to_owned(), "on".to_owned()),
68        ]))
69    }
70}
71
72pub struct PostgresServerHandlerInner {
73    query_handler: ServerSqlQueryHandlerRef,
74    login_verifier: PgLoginVerifier,
75    force_tls: bool,
76    param_provider: Arc<GreptimeDBStartupParameters>,
77
78    session: Arc<Session>,
79    query_parser: Arc<DefaultQueryParser>,
80}
81
82#[derive(Builder)]
83pub(crate) struct MakePostgresServerHandler {
84    query_handler: ServerSqlQueryHandlerRef,
85    user_provider: Option<UserProviderRef>,
86    #[builder(default = "Arc::new(GreptimeDBStartupParameters::new())")]
87    param_provider: Arc<GreptimeDBStartupParameters>,
88    force_tls: bool,
89}
90
91pub(crate) struct PostgresServerHandler(Arc<PostgresServerHandlerInner>);
92
93impl PgWireServerHandlers for PostgresServerHandler {
94    type StartupHandler = PostgresServerHandlerInner;
95    type SimpleQueryHandler = PostgresServerHandlerInner;
96    type ExtendedQueryHandler = PostgresServerHandlerInner;
97    type CopyHandler = NoopCopyHandler;
98    type ErrorHandler = PostgresServerHandlerInner;
99
100    fn simple_query_handler(&self) -> Arc<Self::SimpleQueryHandler> {
101        self.0.clone()
102    }
103
104    fn extended_query_handler(&self) -> Arc<Self::ExtendedQueryHandler> {
105        self.0.clone()
106    }
107
108    fn startup_handler(&self) -> Arc<Self::StartupHandler> {
109        self.0.clone()
110    }
111
112    fn copy_handler(&self) -> Arc<Self::CopyHandler> {
113        Arc::new(NoopCopyHandler)
114    }
115
116    fn error_handler(&self) -> Arc<Self::ErrorHandler> {
117        self.0.clone()
118    }
119}
120
121impl MakePostgresServerHandler {
122    fn make(&self, addr: Option<SocketAddr>) -> PostgresServerHandler {
123        let session = Arc::new(Session::new(addr, Channel::Postgres, Default::default()));
124        let handler = PostgresServerHandlerInner {
125            query_handler: self.query_handler.clone(),
126            login_verifier: PgLoginVerifier::new(self.user_provider.clone()),
127            force_tls: self.force_tls,
128            param_provider: self.param_provider.clone(),
129
130            session: session.clone(),
131            query_parser: Arc::new(DefaultQueryParser::new(self.query_handler.clone(), session)),
132        };
133        PostgresServerHandler(Arc::new(handler))
134    }
135}