Exemplo n.º 1
0
        /// <summary>
        /// Executes the specified query.
        /// </summary>
        /// <param name="query">The query.</param>
        /// <param name="store">Whether or not to download the result set.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception"></exception>
        public MySqlResult Query(string query, bool store = true)
        {
            // list
            List <MySqlResult> results = new List <MySqlResult>();

            // query
            MySqlNative.mysql_real_query(handle, query, (uint)query.Length);

            // check error
            if (Error)
            {
                throw new InvalidOperationException(ErrorString);
            }

            Application.Logger.Log(Logging.LogLevel.Information, "mysql", query);

            while (true)
            {
                if (store)
                {
                    results.Add(new MySqlResult(MySqlNative.mysql_store_result(handle)));
                }
                else
                {
                    results.Add(new MySqlResult(MySqlNative.mysql_store_result(handle)));
                }

                break;
            }

            // exit
            return(results[0]);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a row by fetching it from a result set.
        /// </summary>
        /// <param name="result">The result.</param>
        /// <returns></returns>
        internal static MySqlRow FetchFromResult(MySqlResult result)
        {
            // fetch data
            IntPtr valueArr = MySqlNative.mysql_fetch_row(result.Handle);

            if (valueArr == IntPtr.Zero)
            {
                return(null);
            }

            IntPtr lengthArr = MySqlNative.mysql_fetch_lengths(result.Handle);
            int    numFields = (int)MySqlNative.mysql_num_fields(result.Handle);

            // create row
            MySqlRow row = new MySqlRow();

            row.result = result;
            row.values = new object[numFields];

            // lengths
            long[] length = new long[numFields];
            Marshal.Copy(lengthArr, length, 0, numFields);

            // values
            IntPtr[] valuePtrs = new IntPtr[numFields];
            Marshal.Copy(valueArr, valuePtrs, 0, numFields);

            for (int i = 0; i < numFields; i++)
            {
                row.values[i] = result.Fields[i].CastFrom(Marshal.PtrToStringAnsi(valuePtrs[i]));
            }

            return(row);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Escapes the specified string, removing injection attempts.
        /// </summary>
        /// <param name="str">The query string.</param>
        /// <returns></returns>
        public string Escape(string str)
        {
            // output
            IntPtr to = Marshal.AllocHGlobal((str.Length * 2) + 1);

            // escape
            MySqlNative.mysql_real_escape_string(handle, to, str, (uint)str.Length);

            // convert
            string outStr = Marshal.PtrToStringAnsi(to);

            // free output
            Marshal.FreeHGlobal(to);

            return(outStr);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Fetches the fields for the result set.
        /// </summary>
        private void FetchFields()
        {
            // fetch fields
            IntPtr fieldsArr = MySqlNative.mysql_fetch_fields(handle);
            int    numFields = (int)MySqlNative.mysql_num_fields(handle);

            // create
            fields = new MySqlField[numFields];

            // fields
            for (int i = 0; i < numFields; i++)
            {
                // get field
                MySqlField.Field field = (MySqlField.Field)Marshal.PtrToStructure(fieldsArr + (i * Marshal.SizeOf(typeof(MySqlField.Field))), typeof(MySqlField.Field));

                // add
                fields[i] = new MySqlField(field);
            }
        }
Exemplo n.º 5
0
 public override void Connect(string host, string username, string password, string db, uint port)
 {
     MySqlNative.mysql_real_connect(handle, host, username, password, db, port, IntPtr.Zero, 0);
 }
Exemplo n.º 6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MySqlConnector"/> class.
 /// </summary>
 public MySqlConnector()
 {
     this.handle = MySqlNative.mysql_init(IntPtr.Zero);
 }
Exemplo n.º 7
0
 /// <summary>
 /// Disconnects from the MySql server.
 /// </summary>
 public override void Disconnect()
 {
     MySqlNative.mysql_close(handle);
 }