Register for your free account! | Forgot your password?

You last visited: Today at 14:40

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[Release] Fixed MySql Wrapper

Discussion on [Release] Fixed MySql Wrapper within the CO2 PServer Guides & Releases forum part of the CO2 Private Server category.

Closed Thread
 
Old   #1
 
Spirited's Avatar
 
elite*gold: 12
Join Date: Jul 2011
Posts: 8,214
Received Thanks: 4,118
[Release] Fixed MySql Wrapper

Hey everyone.

This was a request from a close friend of mine who's having issues with his mysql wrapper from Impulse's source.

MySql tables, columns, and values can have spaces... so here's a wrapper that fixes any issues that you might have with spaces (note: the structure was coded by Impulse, all I did was fix it):

Code:
public class MySqlCommand
    {
        private MySqlCommandType _type;
        public MySqlCommandType Type
        {
            get { return _type; }
            set { _type = value; }
        }
        protected string _command;
        public string Command
        {
            get { return _command; }
            set { _command = value; }
        }
        private bool firstPart = true;
        private Dictionary<byte, string> Fields;
        private Dictionary<byte, long> longValues;
        private Dictionary<byte, ulong> ulongValues;
        private Dictionary<byte, bool> boolValues;
        private Dictionary<byte, string> stringValues;
        private byte lastpair;
        public MySqlCommand(MySqlCommandType Type)
        {
            this.Type = Type;
            switch (Type)
            {
                case MySqlCommandType.SELECT:
                    {
                        _command = "SELECT * FROM <R>";
                        break;
                    }
                case MySqlCommandType.UPDATE:
                    {
                        _command = "UPDATE <R> SET ";
                        break;
                    }
                case MySqlCommandType.INSERT:
                    {
                        Fields = new Dictionary<byte, string>();
                        longValues = new Dictionary<byte, long>();
                        ulongValues = new Dictionary<byte, ulong>();
                        boolValues = new Dictionary<byte, bool>();
                        stringValues = new Dictionary<byte, string>();
                        lastpair = 0;
                        _command = "INSERT INTO <R> (<F>) VALUES (<V>)";
                        break;
                    }
                case MySqlCommandType.DELETE:
                    {
                        _command = "DELETE FROM <R> WHERE <C> = <V>";
                        break;
                    }
                case MySqlCommandType.COUNT:
                    {
                        _command = "SELECT count(<V>) FROM <R>";
                        break;
                    }
            }
        }

        #region Select
        public MySqlCommand Select(string table)
        {
            _command = _command.Replace("<R>", "`" + table + "`");
            return this;
        }
        #endregion

        #region Count
        public MySqlCommand Count(string table)
        {
            _command = _command.Replace("<R>", "`" + table + "`");
            return this;
        }
        #endregion

        #region Delete
        public MySqlCommand Delete(string table, string column, string value)
        {
            _command = _command.Replace("<R>", "`" + table + "`");
            _command = _command.Replace("<C>", "`" + column + "`");
            _command = _command.Replace("<V>", "'" + value + "'");
            return this;
        }
        public MySqlCommand Delete(string table, string column, long value)
        {
            _command = _command.Replace("<R>", "`" + table + "`");
            _command = _command.Replace("<C>", "`" + column + "`");
            _command = _command.Replace("<V>", value.ToString());
            return this;
        }
        public MySqlCommand Delete(string table, string column, ulong value)
        {
            _command = _command.Replace("<R>", "`" + table + "`");
            _command = _command.Replace("<C>", "`" + column + "`");
            _command = _command.Replace("<V>", value.ToString());
            return this;
        }
        public MySqlCommand Delete(string table, string column, bool value)
        {
            _command = _command.Replace("<R>", "`" + table + "`");
            _command = _command.Replace("<C>", "`" + column + "`");
            _command = _command.Replace("<V>", (value ? "1" : "0"));
            return this;
        }
        public MySqlCommand COUNT(string table)
        {
            _command = _command.Replace("<R>", "`" + table + "`");
            return this;
        }
        #endregion

        private bool Comma()
        {
            if (firstPart)
            {
                firstPart = false;
                return false;
            }
            if (_command[_command.Length - 1] == ',' || _command[_command.Length - 2] == ',' || _command[_command.Length - 3] == ',')
                return false;
            return true;
        }

        #region Update
        public MySqlCommand Update(string table)
        {
            _command = _command.Replace("<R>", "`" + table + "`");
            return this;
        }
        public MySqlCommand Set(string column, long value)
        {
            if (Type == MySqlCommandType.UPDATE)
            {
                if (Comma())
                    _command = _command + ",`" + column + "` = " + value.ToString() + " ";
                else
                    _command = _command + "`" + column + "` = " + value.ToString() + " ";
            }
            return this;
        }
        public MySqlCommand Set(string column, ulong value)
        {
            if (Type == MySqlCommandType.UPDATE)
            {
                bool comma = false;
                comma = (_command[_command.Length - 1] == ',' || _command[_command.Length - 2] == ',' || _command[_command.Length - 3] == ',') ? false : true;
                if (comma)
                    _command = _command + ",`" + column + "` = " + value.ToString() + " ";
                else
                    _command = _command + "`" + column + "` = " + value.ToString() + " ";
            }
            return this;
        }
        public MySqlCommand Set(string column, string value)
        {
            if (Type == MySqlCommandType.UPDATE)
            {
                if (Comma())
                    _command = _command + ",`" + column + "` = '" + value + "' ";
                else
                    _command = _command + "`" + column + "` = '" + value + "' ";
            }
            return this;
        }
        public MySqlCommand Set(string column, bool value)
        {
            if (Type == MySqlCommandType.UPDATE)
            {
                if (Comma())
                    _command = _command + ",`" + column + "` = " + (value ? "1" : "0") + " ";
                else
                    _command = _command + "`" + column + "` = " + (value ? "1" : "0") + " ";
            }
            return this;
        }
        #endregion

        #region Insert
        public MySqlCommand Insert(string table)
        {
            _command = _command.Replace("<R>", "`" + table + "`");
            return this;
        }
        public MySqlCommand Insert(string field, long value)
        {
            Fields.Add(lastpair, field);
            longValues.Add(lastpair, value);
            lastpair++;
            return this;
        }
        public MySqlCommand Insert(string field, ulong value)
        {
            Fields.Add(lastpair, field);
            ulongValues.Add(lastpair, value);
            lastpair++;
            return this;
        }
        public MySqlCommand Insert(string field, bool value)
        {
            Fields.Add(lastpair, field);
            boolValues.Add(lastpair, value);
            lastpair++;
            return this;
        }
        public MySqlCommand Insert(string field, string value)
        {
            Fields.Add(lastpair, field);
            stringValues.Add(lastpair, value);
            lastpair++;
            return this;
        }
        #endregion

        #region Where
        public MySqlCommand Where(string column, long value)
        {
            _command = _command + "WHERE `" + column + "` = " + value;
            return this;
        }
        public MySqlCommand Where(string column, long value, bool greater)
        {
            if (greater)
                _command = _command + "WHERE `" + column + "` > " + value;
            else
                _command = _command + "WHERE `" + column + "` < " + value;
            return this;
        }
        public MySqlCommand Where(string column, ulong value)
        {
            _command = _command + "WHERE `" + column + "` = " + value;
            return this;
        }
        public MySqlCommand Where(string column, string value)
        {
            _command = _command + "WHERE `" + column + "` = '" + value + "'";
            return this;
        }
        public MySqlCommand Where(string column, bool value)
        {
            _command = _command + "WHERE `" + column + "` = " + (value ? "1" : "0");
            return this;
        }
        #endregion

        #region And
        public MySqlCommand And(string column, long value)
        {
            _command = _command + " AND `" + column + "` = " + value;
            return this;
        }
        public MySqlCommand And(string column, ulong value)
        {
            _command = _command + " AND `" + column + "` = " + value;
            return this;
        }
        public MySqlCommand And(string column, string value)
        {
            _command = _command + " AND `" + column + "` = '" + value + "'";
            return this;
        }
        public MySqlCommand And(string column, bool value)
        {
            _command = _command + " AND `" + column + "` = " + (value ? "1" : "0");
            return this;
        }
        #endregion

        #region Order
        public MySqlCommand Order(string column)
        {
            _command = _command + "ORDER BY " + column + "";
            return this;
        }
        #endregion

        public void Execute()
        {
            if (Type == MySqlCommandType.INSERT)
            {
                string fields = "";
                string values = "";
                byte x;
                for (x = 0; x < lastpair; x++)
                {
                    bool comma = (x + 1) == lastpair ? false : true;
                    #region Fields
                    if (comma)
                        fields += Fields[x] + ",";
                    else
                        fields += Fields[x];
                    #endregion
                    #region Values
                    if (longValues.ContainsKey(x))
                    {
                        if (comma)
                            values += longValues[x].ToString() + ",";
                        else
                            values += longValues[x].ToString();
                    }
                    else if (ulongValues.ContainsKey(x))
                    {
                        if (comma)
                            values += ulongValues[x].ToString()[x] + ",";
                        else
                            values += ulongValues[x].ToString();
                    }
                    else if (boolValues.ContainsKey(x))
                    {
                        if (comma)
                            values += (boolValues[x] ? "1" : "0") + ",";
                        else
                            values += (boolValues[x] ? "1" : "0");
                    }
                    else if (stringValues.ContainsKey(x))
                    {
                        if (comma)
                            values += "'" + stringValues[x] + "'" + ",";
                        else
                            values += "'" + stringValues[x] + "'";
                    }
                    #endregion
                }
                _command = _command.Replace("<F>", fields);
                _command = _command.Replace("<V>", values);
            }
            using (MySqlConnection conn = DataHolder.MySqlConnection)
            {
                conn.Open();
                MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(Command, conn);
                cmd.ExecuteNonQuery();
            }
        }
        public void UpdateInsert()
        {
            if (Type == MySqlCommandType.INSERT)
            {
                string fields = "";
                string values = "";
                byte x;
                for (x = 0; x < lastpair; x++)
                {
                    bool comma = (x + 1) == lastpair ? false : true;
                    #region Fields
                    if (comma)
                        fields += Fields[x] + ",";
                    else
                        fields += Fields[x];
                    #endregion
                    #region Values
                    if (longValues.ContainsKey(x))
                    {
                        if (comma)
                            values += longValues[x].ToString() + ",";
                        else
                            values += longValues[x].ToString();
                    }
                    else if (ulongValues.ContainsKey(x))
                    {
                        if (comma)
                            values += ulongValues[x].ToString()[x] + ",";
                        else
                            values += ulongValues[x].ToString();
                    }
                    else if (boolValues.ContainsKey(x))
                    {
                        if (comma)
                            values += (boolValues[x] ? "1" : "0") + ",";
                        else
                            values += (boolValues[x] ? "1" : "0");
                    }
                    else if (stringValues.ContainsKey(x))
                    {
                        if (comma)
                            values += "'" + stringValues[x] + "'" + ",";
                        else
                            values += "'" + stringValues[x] + "'";
                    }
                    #endregion
                }
                _command = _command.Replace("<F>", fields);
                _command = _command.Replace("<V>", values);
            }
        }
        private string _lasterror = null;
        public string LastError
        {
            get
            {
                return _lasterror;
            }
            set
            {
                _lasterror = value;
            }
        }
    }
    public enum MySqlCommandType
    {
        DELETE, INSERT, SELECT, UPDATE, COUNT
    }
Sincerely,
Fang
Spirited is offline  
Thanks
4 Users
Old 02/08/2012, 22:27   #2
 
xBlackPlagu3x's Avatar
 
elite*gold: 0
Join Date: Jan 2011
Posts: 286
Received Thanks: 71
Thank you Fang! THAT was the problem with my source! I was using Impulse's MySQL method and my tables had spaces!
xBlackPlagu3x is offline  
Old 02/08/2012, 23:15   #3
 
killersub's Avatar
 
elite*gold: 0
Join Date: May 2009
Posts: 884
Received Thanks: 211
I am very fond of the source and the coding style he uses, therefore thanks .

I will be using this.
killersub is offline  
Old 02/08/2012, 23:28   #4
 
elite*gold: 0
Join Date: Dec 2011
Posts: 1,537
Received Thanks: 785
NHibernate
I don't have a username is offline  
Old 02/08/2012, 23:32   #5
 
Spirited's Avatar
 
elite*gold: 12
Join Date: Jul 2011
Posts: 8,214
Received Thanks: 4,118
Quote:
Originally Posted by I don't have a username View Post
NHibernate
I don't use MySql. It's terrible.
I use NHibernate with Ms Sql.
Spirited is offline  
Old 03/23/2012, 23:10   #6
 
bryce16's Avatar
 
elite*gold: 0
Join Date: Oct 2010
Posts: 47
Received Thanks: 3
Random but good-to-know question:
-Does this somehow benefit testers from not being able to login after a sertain time?(I'm currently having that issue so yea =p)

EDIT: I ran across this issue on the ConquerItemTable:"Cannot implicitly convert type 'void' to 'int'."

Quote:
int res = cmd.Update("items").Set("Position", 0).Where("EntityID", EntityID).And("Position", position).Execute();
Quote:
int res = cmd.Update("items").Set("SocketOne", (byte)Item.SocketOne)
.Set("SocketTwo", (byte)Item.SocketTwo).Where("UID", Item.UID).Execute();
bryce16 is offline  
Old 03/23/2012, 23:25   #7
 
Spirited's Avatar
 
elite*gold: 12
Join Date: Jul 2011
Posts: 8,214
Received Thanks: 4,118
Quote:
Originally Posted by bryce16 View Post
Random but good-to-know question:
-Does this somehow benefit testers from not being able to login after a sertain time?(I'm currently having that issue so yea =p)

EDIT: I ran across this issue on the ConquerItemTable:"Cannot implicitly convert type 'void' to 'int'."
Noo, I know what you're thinking though. Here's something I developed to fix connection problems (where the connection maxes out):

Also, with the conversion thing, let me help you break that down.
So you're starting with this:
Code:
int res = cmd.Update("items").Set("SocketOne", (byte)Item.SocketOne)
.Set("SocketTwo", (byte)Item.SocketTwo).Where("UID", Item.UID).Execute();
If you replace the set and where strings with "X", it looks like this:
Code:
int res = cmd.Update("items").Set("X").Set("X").Where("X").Execute();
And if you simplify that even more and look at the end product of what it wants to do... it's creating a command that'll update the table "items" and execute as so:

Code:
int res = cmd.Execute();
See what's wrong with that yet? Execute doesn't return an int. It returns nothing (not null, but void since it's a void function). Hopefully that makes more sense.
Spirited is offline  
Thanks
1 User
Old 03/23/2012, 23:32   #8
 
bryce16's Avatar
 
elite*gold: 0
Join Date: Oct 2010
Posts: 47
Received Thanks: 3
Edit: I got the connection stability part done, so thanks for that, as for the conversion thing, I'm a little stuck with converting those lines of codes.
bryce16 is offline  
Old 09/22/2013, 14:08   #9
 
shadowman123's Avatar
 
elite*gold: 0
Join Date: Aug 2007
Posts: 1,525
Received Thanks: 230
well i dont like the way its coded .. and it got a problem which is the connection is still Alive After the Query is Executed and thats completely wrong ....
shadowman123 is offline  
Old 09/22/2013, 17:06   #10
 
Smallxmac's Avatar
 
elite*gold: 0
Join Date: Dec 2010
Posts: 105
Received Thanks: 13
That is why he uses his kill connection
Smallxmac is offline  
Old 09/22/2013, 17:29   #11
 
shadowman123's Avatar
 
elite*gold: 0
Join Date: Aug 2007
Posts: 1,525
Received Thanks: 230
KillConnection isnt the Correct way of Handling the Situation ... cuz u Run its codes Through Threading which consumes CPU Cycles Raising the Cpu Usage...so thats not Right .. what u should do is Close the connection everytime Query is Excuted .y would it be Opened for no reason ?
shadowman123 is offline  
Old 09/22/2013, 21:52   #12
 
Spirited's Avatar
 
elite*gold: 12
Join Date: Jul 2011
Posts: 8,214
Received Thanks: 4,118
I made a post that you bumped explaining exactly why I was wrong in this post.
Closed, please don't bump multiple threads about the same problem.
Spirited is offline  
Closed Thread


Similar Threads Similar Threads
au3 wrapper
12/23/2008 - Guild Wars - 11 Replies
hi ganz dumme fragen^^ wo gibst den neuen au3 wrapper zum download bei google habe ich ncihts gefunden oder ich bin einfach zu blöd zum richtig googlen kann mir wer weiter helfen??
Empty wrapper help
01/18/2008 - WoW PServer Exploits, Hacks & Tools - 1 Replies
I play on wowfusion, the only wrapping paper i can get is the empty wrapper. But everytime i try to wrap something i get : Wrapped item can't be wrap. What can i do? I tryed a lot of item and i always get this same message. I tryed slot switching , other bags and ... Any help would be appreciated. Thx
Forum MySQL error fixed
09/09/2006 - Main - 11 Replies
Ich habe zwar keine Ahnung wie der Fehler zustande kam, aber ich vermute mal das mein Host an php rumgespielt hat, musste zum glück nur eine query anpassen damit es wieder ging. Sorry für die Downtime Sofern euch weitere Fehler auffallen bitte bei mir melden, danke. - Lwy



All times are GMT +2. The time now is 14:40.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.