chunshao90 commented on code in PR #1439: URL: https://github.com/apache/incubator-horaedb/pull/1439#discussion_r1452136189
########## server/src/session.rs: ########## @@ -0,0 +1,223 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// Forked from https://github.com/GreptimeTeam/greptimedb/blob/ca4d690424b03806ea0f8bd5e491585224bbf220/src/session/src/lib.rs + +// Copyright 2023 Greptime Team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::{ + fmt::{Display, Formatter}, + net::SocketAddr, + sync::Arc, +}; + +use arc_swap::ArcSwap; +use catalog::consts::{DEFAULT_CATALOG, DEFAULT_SCHEMA}; +use sqlparser::dialect::{Dialect, MySqlDialect, PostgreSqlDialect}; + +/// Session for persistent connection such as MySQL, PostgreSQL etc. +#[derive(Debug)] +pub struct Session { + catalog: ArcSwap<String>, + schema: ArcSwap<String>, + conn_info: ConnInfo, +} + +pub type SessionRef = Arc<Session>; + +impl Session { + pub fn new(addr: Option<SocketAddr>, channel: Channel) -> Self { + Session { + catalog: ArcSwap::new(Arc::new(DEFAULT_CATALOG.into())), + schema: ArcSwap::new(Arc::new(DEFAULT_SCHEMA.into())), + conn_info: ConnInfo::new(addr, channel), + } + } + + #[inline] + #[allow(dead_code)] + pub fn conn_info(&self) -> &ConnInfo { + &self.conn_info + } + + #[inline] + pub fn catalog(&self) -> String { + self.catalog.load().to_string() + } + + #[inline] + pub fn schema(&self) -> String { + self.schema.load().to_string() + } + + #[inline] + pub fn set_catalog(&self, catalog: String) { + self.catalog.store(Arc::new(catalog)); + } + + #[inline] + pub fn set_schema(&self, schema: String) { + self.schema.store(Arc::new(schema)); + } +} + +#[derive(Debug)] +pub struct ConnInfo { + pub client_addr: Option<SocketAddr>, + pub channel: Channel, +} + +impl Display for ConnInfo { + fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { + write!( + f, + "{}[{}]", + self.channel, + self.client_addr + .map(|addr| addr.to_string()) + .as_deref() + .unwrap_or("unknown client addr") + ) + } +} + +impl ConnInfo { + pub fn new(client_addr: Option<SocketAddr>, channel: Channel) -> Self { + Self { + client_addr, + channel, + } + } +} + +#[derive(Debug, PartialEq)] +#[allow(dead_code)] Review Comment: > Do we have a plan to support different channels and dialects? If not, we could remove some unused codes. Want to support `pgsql` dialect later. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
