| 87 | } |
| 88 | |
| 89 | func (s *Session) Open(enableRPCCompression bool, connectionTimeoutInMs int) error { |
| 90 | if s.config.FetchSize <= 0 { |
| 91 | s.config.FetchSize = DefaultFetchSize |
| 92 | } |
| 93 | if s.config.TimeZone == "" { |
| 94 | s.config.TimeZone = DefaultTimeZone |
| 95 | } |
| 96 | |
| 97 | if s.config.ConnectRetryMax <= 0 { |
| 98 | s.config.ConnectRetryMax = DefaultConnectRetryMax |
| 99 | } |
| 100 | |
| 101 | var err error |
| 102 | |
| 103 | // in thrift 0.14.1, this func returns two values; in thrift 0.15.0, it returns one. |
| 104 | s.trans = thrift.NewTSocketConf(net.JoinHostPort(s.config.Host, s.config.Port), &thrift.TConfiguration{ |
| 105 | ConnectTimeout: time.Duration(connectionTimeoutInMs) * time.Millisecond, // Use 0 for no timeout |
| 106 | }) |
| 107 | // s.trans = thrift.NewTFramedTransport(s.trans) // deprecated |
| 108 | tmp_conf := thrift.TConfiguration{MaxFrameSize: thrift.DEFAULT_MAX_FRAME_SIZE} |
| 109 | s.trans = thrift.NewTFramedTransportConf(s.trans, &tmp_conf) |
| 110 | if !s.trans.IsOpen() { |
| 111 | err = s.trans.Open() |
| 112 | if err != nil { |
| 113 | return err |
| 114 | } |
| 115 | } |
| 116 | s.protocolFactory = getProtocolFactory(enableRPCCompression) |
| 117 | iprot := s.protocolFactory.GetProtocol(s.trans) |
| 118 | oprot := s.protocolFactory.GetProtocol(s.trans) |
| 119 | s.client = rpc.NewIClientRPCServiceClient(thrift.NewTStandardClient(iprot, oprot)) |
| 120 | req := rpc.TSOpenSessionReq{ |
| 121 | ClientProtocol: rpc.TSProtocolVersion_IOTDB_SERVICE_PROTOCOL_V3, ZoneId: s.config.TimeZone, Username: s.config.UserName, |
| 122 | Password: &s.config.Password, |
| 123 | } |
| 124 | req.Configuration = make(map[string]string) |
| 125 | req.Configuration["sql_dialect"] = s.config.sqlDialect |
| 126 | if s.config.Version == "" { |
| 127 | req.Configuration["version"] = string(DEFAULT_VERSION) |
| 128 | } else { |
| 129 | req.Configuration["version"] = string(s.config.Version) |
| 130 | } |
| 131 | if s.config.Database != "" { |
| 132 | req.Configuration["db"] = s.config.Database |
| 133 | } |
| 134 | resp, err := s.client.OpenSession(context.Background(), &req) |
| 135 | if err != nil { |
| 136 | return err |
| 137 | } |
| 138 | s.sessionId = resp.GetSessionId() |
| 139 | s.requestStatementId, err = s.client.RequestStatementId(context.Background(), s.sessionId) |
| 140 | if timeFactor, err := getTimeFactor(resp); err != nil { |
| 141 | return err |
| 142 | } else { |
| 143 | s.timeFactor = timeFactor |
| 144 | } |
| 145 | return err |
| 146 | } |