internal void ExecuteControlSql(string sql) { ToDatabase.WriteLine("X" + sql); ToDatabase.Flush(); while (true) { var line = this.FromDatabase.ReadLine(); if (line == null) { throw new IOException("Connection to server lost!"); } switch ((byte)line[0]) { case (byte)DbLineType.Prompt: return; case (byte)DbLineType.Error: throw new MonetDbException(line.Substring(1)); } } }
/// <summary> /// Connects to a given host. Returns a list of any warnings from the server. /// </summary> /// <param name="host"></param> /// <param name="port"></param> /// <param name="username"></param> /// <param name="password"></param> /// <param name="database"></param> /// <returns></returns> public IList <string> Connect(string host, int port, string username, string password, string database) { Database = database; Host = host; Port = port; Username = username; _socket = new TcpClient(Host, Port) { NoDelay = true, ReceiveTimeout = 60 * 2 * 1000, SendBufferSize = 60 * 2 * 1000 }; this.FromDatabase = new StreamReader(new Stream(_socket.GetStream())); this.ToDatabase = new StreamWriter(new Stream(_socket.GetStream())) { NewLine = "\n" }; var challenge = FromDatabase.ReadLine(); // wait till the prompt FromDatabase.ReadLine(); var response = GetChallengeResponse(challenge, username, password, "sql", database, null); ToDatabase.WriteLine(response); ToDatabase.Flush(); var temp = FromDatabase.ReadLine(); var redirects = new List <string>(); var warnings = new List <string>(); while (temp != ".") { if (string.IsNullOrEmpty(temp)) { throw new MonetDbException("Connection to the server was lost"); } switch ((DbLineType)temp[0]) { case DbLineType.Error: throw new MonetDbException(temp.Substring(1)); case DbLineType.Info: warnings.Add(temp.Substring(1)); break; case DbLineType.Redirect: warnings.Add(temp.Substring(1)); break; } temp = FromDatabase.ReadLine(); } if (redirects.Count <= 0) { return(warnings); } _socket.Client.Close(); _socket.Close(); return(FollowRedirects(redirects, username, password)); }