Example #1
0
 public TreeOfConnections(Table branchTable, TreeOfConnections parent)
 {
     branches           = new List <TreeOfConnections>();
     currentBranchTable = branchTable;
     levelOfTree        = parent.levelOfTree + 1;
     parentNode         = parent;
 }
Example #2
0
 private void setPrefixes(TreeOfConnections currentNode)
 {
     currentNode.currentBranchTable.tableName = "PlayersMain" + currentNode.currentBranchTable.tableName;
     foreach (TreeOfConnections item in currentNode.branches)
     {
         setPrefixesMinor(item);
     }
 }
Example #3
0
        /// <summary>
        /// DO NOT CALL IT ON YOUR OWN! It's a part of createMajorProcedureForPlayer, and it's not supposed to be used alone
        /// </summary>
        /// <param name="currentNode"></param>
        /// <param name="currentQuery"></param>
        private void createProcedureForPlayer(TreeOfConnections currentNode, Query currentQuery)
        {
            switch (currentNode.connectionTypeToUpperLevel)
            {
            case ConnectionsInTables.ConnectionType.OTO:
            {
                currentQuery.tables += ", " + currentNode.parentNode.currentBranchTable.tableName;
                currentQuery.wheres += " and "
                                       + currentNode.currentBranchTable.tableName + ".id_For" + currentNode.parentNode.currentBranchTable.tableName
                                       + "  = " + currentNode.parentNode.currentBranchTable.tableName + ".id_For" + currentNode.currentBranchTable.tableName;

                break;
            }

            case ConnectionsInTables.ConnectionType.OTM:
            {
                currentQuery.tables += ", " + currentNode.parentNode.currentBranchTable.tableName;
                currentQuery.wheres += " and "
                                       + currentNode.currentBranchTable.tableName + ".id_"
                                       + "  = " + currentNode.parentNode.currentBranchTable.tableName + ".id_For" + currentNode.currentBranchTable.tableName;
                break;
            }

            case ConnectionsInTables.ConnectionType.MTO:
            {
                currentQuery.tables += ", " + currentNode.parentNode.currentBranchTable.tableName;
                currentQuery.wheres += " and "
                                       + currentNode.currentBranchTable.tableName + ".id_For" + currentNode.parentNode.currentBranchTable.tableName
                                       + "  = " + currentNode.parentNode.currentBranchTable.tableName + ".id_";
                break;
            }

            case ConnectionsInTables.ConnectionType.MTM:
            {
                string connectorTableName = "Connector" + currentNode.parentNode.currentBranchTable.tableName + currentNode.currentBranchTable.tableName;
                currentQuery.tables += ", " + currentNode.parentNode.currentBranchTable.tableName + ", " + connectorTableName;
                currentQuery.wheres += " and " +
                                       connectorTableName + ".id_For" + currentNode.parentNode.currentBranchTable.tableName + " = "
                                       + currentNode.parentNode.currentBranchTable.tableName + ".id_ and " +
                                       connectorTableName + ".id_For" + currentNode.currentBranchTable.tableName + " = "
                                       + currentNode.currentBranchTable.tableName + ".id_";
                break;
            }

            default:
            {
                throw new Exception("Wrong tree structure");
            }
            }
            if (currentNode.levelOfTree > 1)
            {
                createProcedureForPlayer(currentNode.parentNode, currentQuery);
            }
        }
Example #4
0
        private void addIDs(TreeOfConnections connectionsInTables)
        {
            Column col = new Column();

            col.columnName = "id_";
            col.type       = Column.ColumnType.Number;
            connectionsInTables.currentBranchTable.addColumn(col);
            foreach (TreeOfConnections con in connectionsInTables.branches)
            {
                addIDs(con);
            }
        }
Example #5
0
 private void createDatabase(TreeOfConnections connections)
 {
     //Creating database
     try
     {
         DBCreator db = new DBCreator(connections, databaseName);
     }
     catch (Exception e)
     {
         MessageBox.Show(e.Message, "Error while creating database", MessageBoxButtons.OK);
         return;
     }
     registerdatabaseCreated(databaseName);
 }
Example #6
0
 private void setPrefixesMinor(TreeOfConnections currentNode)
 {
     if (currentNode.connectionTypeToUpperLevel != ConnectionsInTables.ConnectionType.GA)
     {
         currentNode.currentBranchTable.tableName = "PlayersSub" + currentNode.currentBranchTable.tableName;
     }
     else
     {
         currentNode.currentBranchTable.tableName = "Items" + currentNode.currentBranchTable.tableName;
     }
     foreach (TreeOfConnections item in currentNode.branches)
     {
         setPrefixesMinor(item);
     }
 }
Example #7
0
        private Query createProcedureForItems(TreeOfConnections currentNode)
        {
            foreach (Column col in currentNode.currentBranchTable.columnsInTable)
            {
                currentNode.parentNode.currentBranchTable.addColumn(col);
            }
            Query currentQuery = new Query(false);

            currentQuery.queryName = "GetItemsFor" + currentNode.parentNode.currentBranchTable.tableName;
            if (currentNode.parentNode.currentBranchTable.tableName == "Players")
            {
                currentQuery.queryName = "GetItemsFor" + currentNode.parentNode.currentBranchTable.tableName;
            }
            currentQuery.select = "" + currentNode.currentBranchTable.tableName;
            return(currentQuery);
        }
Example #8
0
 private void makeConnectionInDatabase(List <Query> querries, TreeOfConnections node)
 {
     if (node.levelOfTree > 0)
     {
         if (node.currentBranchTable.tableName.StartsWith("Items"))
         {
             querries.Add(createProcedureForItems(node));
         }
         else
         {
             querries.Add(createMajorProcedureForPlayer(node));
         }
     }
     foreach (TreeOfConnections connection in node.branches)
     {
         makeConnectionInDatabase(querries, connection);
     }
 }
Example #9
0
        public DBCreator(TreeOfConnections connectionsInTables, string dataName)
        {
            List <Query> queriesToDatabase = new List <Query>();

            AdditionalTablesToCreate = new List <Table>();
            additionalForeignKeys    = new List <string>();
            dataName = "RPGH" + dataName;
            createDatabase(dataName);
            setPrefixes(connectionsInTables);
            makeConnectionInDatabase(queriesToDatabase, connectionsInTables);
            addIDs(connectionsInTables);
            connectionWithDB.Open();
            createTables(connectionsInTables);
            createAdditionalTables();
            createProcedures(queriesToDatabase);
            addForeignKeys();
            connectionWithDB.Close();
        }
Example #10
0
        private void buttonCreateDatabase_Click(object sender, EventArgs e)
        {
            makeConnections();
            Table tmp = new Table();

            foreach (Table plTable in playerTablesToUse)
            {
                if (plTable.tableName == "Players")
                {
                    tmp = plTable;
                    break;
                }
            }
            TreeOfConnections tree = new TreeOfConnections(tmp);

            if (!tree.setConnections(connectionsToCreate))
            {
                MessageBox.Show("Some of connections were not possible to create", "Creation error", MessageBoxButtons.OK);
                return;
            }
            foreach (Table tab in playerTablesToUse)
            {
                if (!tree.isInTree(tab))
                {
                    MessageBox.Show("Some of player tables are not connected!", "Creation error", MessageBoxButtons.OK);
                    return;
                }
            }
            foreach (Table tab in itemTablesToUse)
            {
                if (!tree.isInTree(tab))
                {
                    MessageBox.Show("Some of item tables are not connected!", "Creation error", MessageBoxButtons.OK);
                    return;
                }
            }
            registerCreateDatabase(tree);
        }
Example #11
0
        /// <summary>
        /// Create ID's for connection and create
        /// </summary>
        /// <param name="currentNode"></param>
        /// <returns></returns>
        private Query createMajorProcedureForPlayer(TreeOfConnections currentNode)
        {
            Query currentQuery = new Query(true);

            switch (currentNode.connectionTypeToUpperLevel)
            {
            case ConnectionsInTables.ConnectionType.OTO:
            {
                currentQuery.tables += currentNode.parentNode.currentBranchTable.tableName + ", " + currentNode.currentBranchTable.tableName;
                currentQuery.wheres +=
                    currentNode.currentBranchTable.tableName + ".id_For" + currentNode.parentNode.currentBranchTable.tableName
                    + "  = " + currentNode.parentNode.currentBranchTable.tableName + ".id_For" + currentNode.currentBranchTable.tableName;
                currentQuery.select    = currentNode.currentBranchTable.tableName + ".* ";
                currentQuery.queryName = "ShowForPlayer" + currentNode.currentBranchTable.tableName;
                Column col = new Column();
                col.columnName = "id_For" + currentNode.parentNode.currentBranchTable.tableName;
                col.type       = Column.ColumnType.Number;
                currentNode.currentBranchTable.addColumn(col);
                col            = new Column();
                col.columnName = "id_For" + currentNode.currentBranchTable.tableName;
                col.type       = Column.ColumnType.Number;
                currentNode.parentNode.currentBranchTable.addColumn(col);
                break;
            }

            case ConnectionsInTables.ConnectionType.OTM:
            {
                currentQuery.tables += currentNode.parentNode.currentBranchTable.tableName + ", " + currentNode.currentBranchTable.tableName;
                currentQuery.wheres +=
                    currentNode.currentBranchTable.tableName + ".id_"
                    + "  = " + currentNode.parentNode.currentBranchTable.tableName + ".id_For" + currentNode.currentBranchTable.tableName;
                currentQuery.select    = currentNode.currentBranchTable.tableName + ".* ";
                currentQuery.queryName = "ShowForPlayer" + currentNode.currentBranchTable.tableName;
                Column col = new Column();
                col.columnName = "id_For" + currentNode.currentBranchTable.tableName;
                col.type       = Column.ColumnType.Number;
                currentNode.parentNode.currentBranchTable.addColumn(col);
                break;
            }

            case ConnectionsInTables.ConnectionType.MTO:
            {
                currentQuery.tables += currentNode.parentNode.currentBranchTable.tableName + ", " + currentNode.currentBranchTable.tableName;
                currentQuery.wheres +=
                    currentNode.currentBranchTable.tableName + ".id_For" + currentNode.parentNode.currentBranchTable.tableName
                    + "  = " + currentNode.parentNode.currentBranchTable.tableName + ".id_";
                currentQuery.select    = currentNode.currentBranchTable.tableName + ".* ";
                currentQuery.queryName = "ShowForPlayer" + currentNode.currentBranchTable.tableName;
                Column col = new Column();
                col.columnName = "id_For" + currentNode.parentNode.currentBranchTable.tableName;
                col.type       = Column.ColumnType.Number;
                currentNode.currentBranchTable.addColumn(col);
                break;
            }

            case ConnectionsInTables.ConnectionType.MTM:
            {
                string connectorTableName = "Connector" + currentNode.parentNode.currentBranchTable.tableName + currentNode.currentBranchTable.tableName;
                currentQuery.tables += currentNode.parentNode.currentBranchTable.tableName + ", " + connectorTableName + ", " + currentNode.currentBranchTable.tableName;
                currentQuery.wheres +=
                    connectorTableName + ".id_For" + currentNode.parentNode.currentBranchTable.tableName + " = "
                    + currentNode.parentNode.currentBranchTable.tableName + ".id_ and " +
                    connectorTableName + ".id_For" + currentNode.currentBranchTable.tableName + " = "
                    + currentNode.currentBranchTable.tableName + ".id_";
                currentQuery.select    = currentNode.currentBranchTable.tableName + ".* ";
                currentQuery.queryName = "ShowForPlayer" + currentNode.currentBranchTable.tableName;
                Table tab = new Table();
                tab.tableName = connectorTableName;
                Column col = new Column();
                col.columnName = "id_For" + currentNode.parentNode.currentBranchTable.tableName;
                col.type       = Column.ColumnType.Number;
                tab.addColumn(col);
                col            = new Column();
                col.columnName = "id_For" + currentNode.currentBranchTable.tableName;
                col.type       = Column.ColumnType.Number;
                tab.addColumn(col);
                AdditionalTablesToCreate.Add(tab);
                break;
            }

            default:
            {
                throw new Exception("Wrong tree structure");
            }
            }

            if (currentNode.levelOfTree > 1)
            {
                createProcedureForPlayer(currentNode.parentNode, currentQuery);
            }
            return(currentQuery);
        }
Example #12
0
        private void createTables(TreeOfConnections connections)
        {
            Table  tab     = connections.currentBranchTable;
            string command = "Create table " + tab.tableName + "(";

            foreach (Column col in tab.columnsInTable)
            {
                command += col.columnName;
                if (col.columnName.StartsWith("id_") && (col.columnName != "id_"))
                {
                    string que = "Alter table " + tab.tableName + " add foreign key (" + col.columnName
                                 + ") References " + col.columnName.Substring(6) + "(id_);";
                    additionalForeignKeys.Add(que);
                }
                switch (col.type)
                {
                case Column.ColumnType.Enum:
                {
                    command += " enum(";
                    foreach (string option in col.possibleEnumOptions)
                    {
                        command += "\"" + option + "\"";
                        if (col.possibleEnumOptions.Last() != option)
                        {
                            command += ",";
                        }
                    }
                    command += ")";
                    break;
                }

                case Column.ColumnType.Number:
                {
                    command += " int";
                    break;
                }

                case Column.ColumnType.Text:
                {
                    command += " varchar(100)";
                    break;
                }

                default:
                {
                    throw new Exception("Whoops! Something went wrong!");
                }
                }
                if (col.columnName == "id_")
                {
                    command += " primary key auto_increment";
                }
                if (col != tab.columnsInTable.Last())
                {
                    command += ",";
                }
            }
            command += ");";
            MySqlCommand com = new MySqlCommand(command, connectionWithDB);

            com.ExecuteNonQuery();
            foreach (TreeOfConnections tree in connections.branches)
            {
                createTables(tree);
            }
        }
Example #13
0
        /// <summary>
        /// Tries to set tree of connections
        /// </summary>
        /// <param name="connection">List of connections to place in table</param>
        /// <returns>IF connections are set correctly then it returns true. Otherwise, returns false</returns>
        public bool setConnections(List <ConnectionsInTables> connection)
        {
            List <ConnectionsInTables> unhandledConnections = new List <ConnectionsInTables>(connection);
            int lastNumberOfHandledConnections = unhandledConnections.Count;

            while (unhandledConnections.Count > 0)
            {
                for (int i = 0; i < unhandledConnections.Count;)
                {
                    ConnectionsInTables tmp = unhandledConnections[i];
                    if (tmp.type == ConnectionsInTables.ConnectionType.GA)
                    {
                        if (isInTree(tmp.sourceTable))
                        {
                            TreeOfConnections tmpTreeConnection = getConnection(tmp.sourceTable);
                            TreeOfConnections newBranch         = new TreeOfConnections(tmp.targetTable, tmpTreeConnection);
                            newBranch.connectionTypeToUpperLevel = tmp.type;
                            tmpTreeConnection.branches.Add(newBranch);
                            unhandledConnections.Remove(tmp);
                            continue;
                        }
                    }
                    if (isInTree(tmp.sourceTable))
                    {
                        if (isInTree(tmp.targetTable))
                        {
                            unhandledConnections.Remove(tmp);
                            continue;
                        }
                        TreeOfConnections tmpTreeConnection = getConnection(tmp.sourceTable);
                        TreeOfConnections newBranch         = new TreeOfConnections(tmp.targetTable, tmpTreeConnection);
                        newBranch.connectionTypeToUpperLevel = reverseColumnType(tmp.type);
                        tmpTreeConnection.branches.Add(newBranch);
                        unhandledConnections.Remove(tmp);
                        continue;
                    }
                    else if (isInTree(tmp.targetTable))
                    {
                        TreeOfConnections tmpTreeConnection = getConnection(tmp.targetTable);
                        TreeOfConnections newBranch         = new TreeOfConnections(tmp.sourceTable, tmpTreeConnection);
                        newBranch.connectionTypeToUpperLevel = tmp.type;
                        tmpTreeConnection.branches.Add(newBranch);
                        unhandledConnections.Remove(tmp);
                        continue;
                    }
                    else
                    {
                        i++;
                    }
                }
                if (lastNumberOfHandledConnections > unhandledConnections.Count)
                {
                    lastNumberOfHandledConnections = unhandledConnections.Count;
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }