Ejemplo n.º 1
0
        private static void ExecConnect(string command)
        {
            var cmdArgs  = command.Remove(0, 7).TrimStart().Split(' ');
            var username = string.Empty;
            var server   = string.Empty;
            var database = string.Empty;

            var password = new Regex("\"(.*)\"", RegexOptions.IgnoreCase).Replace(new Regex("-p \".*\"", RegexOptions.IgnoreCase).Match(command).Value.Replace("-p ", ""), "$1");

            for (int i = 0, l = cmdArgs.Length; i < l; ++i)
            {
                switch (cmdArgs[i])
                {
                case "-u":
                    username = cmdArgs[i + 1];
                    break;

                case "-s":
                    server = cmdArgs[i + 1];
                    break;

                case "-d":
                    database = cmdArgs[i + 1];
                    break;
                }
            }

            if (server == string.Empty)
            {
                Console.WriteLine("Cannot connect to a server. No server provided. Use \"-s ServerName\" to give the name of the server to use with the connection.");
            }
            else
            {
                if (username == string.Empty)
                {
                    Console.WriteLine("Cannot connect to a server. No user provided. Use \"-u Username\" to give the username.");
                }
                else
                {
                    try
                    {
                        _db = Jsondb.Connect(server, username, password, database);
                        _userIsConnected = true;
                        _serverName      = server;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Unable to connect to the database: " + e.Message);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private static void ExecMkServer(string command)
        {
            var parts = command.Remove(0, 8).Trim().Split(' ');

            var username = string.Empty;
            var server   = string.Empty;

            var password = new Regex("\"(.*)\"", RegexOptions.IgnoreCase).Replace(new Regex("-p \".*\"", RegexOptions.IgnoreCase).Match(command).Value.Replace("-p ", ""), "$1");

            for (int i = 0, l = parts.Length; i < l; i++)
            {
                switch (parts[i])
                {
                case "-s":
                    server = parts[i + 1];
                    break;

                case "-u":
                    username = parts[i + 1];
                    break;
                }
            }

            while (server == string.Empty)
            {
                Console.Write(" -> Server name: ");
                server = _validateName(Console.ReadLine()?.Trim());
            }

            while (username == string.Empty)
            {
                Console.Write(" -> Username: "******" -> Password (Leave blank to ignore (not recommended)): ");
                password = _password();
            }

            try
            {
                Jsondb.CreateServer(server, username, password);
                Console.WriteLine("Server created.");
            }
            catch (Exception e)
            {
                Console.WriteLine("Unable to create the server: " + e.Message);
            }
        }
Ejemplo n.º 3
0
 private void CreateServer(object sender, RoutedEventArgs e)
 {
     LockUi();
     if (Jsondb.ServerExists(ServerNameBox.Text))
     {
         new MessageWindow(this, "A server with this name already exist.", Title, MessageWindowButton.Ok, MessageWindowImage.Error).Open();
     }
     else
     {
         Jsondb.CreateServer(ServerNameBox.Text, UsernameBox.Text, PasswordBox.Text);
         new MessageWindow(this, "The server is successfully created.", Title, MessageWindowButton.Ok, MessageWindowImage.Success).Open();
         Close();
     }
     UnlockUi();
 }
Ejemplo n.º 4
0
            /// <summary>
            /// Send an enqueued query to the server.
            /// </summary>
            public void Send()
            {
                IWebSocketSession s;

                if (_sessions.TryGetSession(_id, out s))
                {
                    var serverName = _parsedQuery["jsondb-server-name"];
                    if (string.IsNullOrEmpty(serverName))
                    {
                        _sessions.CloseSession(_id, CloseStatusCode.InvalidData, "The request has not a server to connect on.");
                    }

                    else
                    {
                        var databaseName = _parsedQuery["jsondb-database-name"];
                        if (string.IsNullOrEmpty(databaseName))
                        {
                            _sessions.CloseSession(_id, CloseStatusCode.InvalidData, "The request has not a database to connect on.");
                        }
                        else
                        {
                            try
                            {
                                Executing = true;
                                var db = Jsondb.Connect(serverName, _parsedQuery["jsondb-user-name"], _parsedQuery["jsondb-user-password"]);
                                db.SetDatabase(databaseName);
                                var res = db.Query(_parsedQuery["jsondb-query-request"]);
                                _sessions.SendToAsync(res.ToString(), _id, (success) =>
                                {
                                    if (success)
                                    {
                                        Logger.Log("Query executed with ID: " + _id, Logger.LogType.Info);
                                        _sessions.CloseSession(_id, CloseStatusCode.Normal, "Received query executed.");
                                    }
                                    else
                                    {
                                        _sessions.CloseSession(_id, CloseStatusCode.ServerError, "Error when executing the query.");
                                    }
                                });
                            }
                            catch (Exception e)
                            {
                                _sessions.CloseSession(_id, CloseStatusCode.ServerError, e.Message);
                            }
                        }
                    }
                }
            }
Ejemplo n.º 5
0
        private void AddConnection(object sender, RoutedEventArgs e)
        {
            try
            {
                Database tempDb = Jsondb.Connect(ServerNameBox.Text, UsernameBox.Text, PasswordBox.Password, DatabaseNameBox.Text);
                if (tempDb.IsConnected())
                {
                    App.Settings.Connections.Add(
                        ConnectionNameBox.Text + "{{s}}" +
                        ServerNameBox.Text + "{{s}}" +
                        UsernameBox.Text + "{{s}}" +
                        PasswordBox.Password + "{{s}}" +
                        DatabaseNameBox.Text);

                    App.Settings.Save();

                    new MessageWindow(
                        this,
                        "Connection added successfully.",
                        "Connection added",
                        MessageWindowButton.Ok,
                        MessageWindowImage.Success).Open();

                    tempDb.Disconnect();

                    Close();
                }
                else
                {
                    throw new Exception("An error occurred when trying to connect to this server.");
                }
            }
            catch (Exception ex)
            {
                new MessageWindow(
                    this,
                    ex.Message,
                    "Error",
                    MessageWindowButton.Ok,
                    MessageWindowImage.Error).Open();
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Connect to a server.
 /// </summary>
 /// <param name="serverName">The name of the server</param>
 /// <param name="username">The username</param>
 /// <param name="password">The password</param>
 /// <param name="database">The name of the database to use</param>
 public static void Connect(string serverName, string username, string password, string database)
 {
     _db = Jsondb.Connect(serverName, username, password, database);
 }
Ejemplo n.º 7
0
        private static void ExecRmSrever(string command)
        {
            var parts = command.Remove(0, 8).Trim().Split(' ');

            var username = string.Empty;
            var server   = string.Empty;

            var password = new Regex("\"(.*)\"", RegexOptions.IgnoreCase).Replace(new Regex("-p \".*\"", RegexOptions.IgnoreCase).Match(command).Value.Replace("-p ", ""), "$1");

            for (int i = 0, l = parts.Length; i < l; i++)
            {
                switch (parts[i])
                {
                case "-s":
                    server = parts[i + 1];
                    break;

                case "-u":
                    username = parts[i + 1];
                    break;
                }
            }

            while (server == string.Empty)
            {
                Console.Write(" -> Server name: ");
                server = _validateName(Console.ReadLine()?.Trim());
            }

            while (username == string.Empty)
            {
                Console.Write(" -> Username: "******" -> Password: "******"The server will be deleted, note that ALL your databases and tables will be also deleted. It's recommended to backup your server before continue. Are you sure to delete this server ? (Type 'yes' to confirm): ");
                var choice = Console.ReadLine()?.ToLower().Trim();
                if (choice == "yes" || choice == "y")
                {
                    Directory.Delete(Util.MakePath(Util.AppRoot(), "servers", server), true);
                    Configuration.RemoveServer(server);
                    Console.WriteLine("Server deleted.");
                }
                else
                {
                    Console.WriteLine("Server deletion cancelled.");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Unable to delete the server: " + e.Message);
            }
        }