Summary description for PhpMyDbConnection.
Inheritance: PHP.Library.Data.PhpDbConnection
Esempio n. 1
0
        /// <summary>
        /// Sends a query to the current database associated with a specified connection. Preserves binary characters.
        /// </summary>
        /// <param name="query">Query.</param>
        /// <param name="connection">Connection resource.</param>
        /// <returns>Query resource or a <B>null</B> reference (<B>null</B> in PHP) on failure.</returns>
        private static PhpResource QueryBinary(byte[] query, PhpMyDbConnection connection)
        {
            Debug.Assert(query != null);
            Debug.Assert(connection != null && connection.IsValid);

            if (query.Length == 0)
                return null;

            //
            var encoding = Configuration.Application.Globalization.PageEncoding;

            //
            List<IDataParameter> parameters = null;
            string commandText = null;
            int commandTextLast = 0;

            // Parse values whether it contains non-ascii characters,
            // non-encodable values convert to byte[] parameter:
            int lastQuote = -1;
            bool escaped = false;
            bool containsNonAscii = false;  // whether encosing may corrupt value when storing into BLOB column
            int escapedChars = 0;    // amount of '\' chars (> 0 means we have to unescape the value)

            for (int i = 0; i < query.Length; i++)
            {
                byte b = query[i];

                if (b == '\'' || b == '\"')
                {
                    if (escaped)
                    {
                        escaped = false;
                    }
                    else //(!escaped)
                    {
                        if (lastQuote >= 0 && query[lastQuote] != b)
                            continue;   // incompatible quotes (should be escaped, but we should handle that)

                        if (lastQuote >= 0 && containsNonAscii)
                        {
                            commandText = string.Concat(commandText, encoding.GetString(query, commandTextLast, lastQuote - commandTextLast));
                            commandTextLast = i + 1;

                            // param name @paramName
                            string paramName = string.Concat("_ImplData_", i.ToString());

                            // replace [lastQuote, i] with "@paramName"
                            // and add parameter @paramName
                            byte[] value = new byte[i - lastQuote - 1 - escapedChars];
                            if (escapedChars == 0)
                            {
                                // we can block-copy the value, there are no escaped characters:
                                Buffer.BlockCopy(query, lastQuote + 1, value, 0, value.Length);
                            }
                            else
                            {
                                // unescape the value, parameters are assumed to contained raw data, escaping is not desirable:
                                UnescapeString(query, lastQuote + 1, value);
                            }

                            //
                            if (parameters == null) parameters = new List<IDataParameter>(1);
                            parameters.Add(new MySqlParameter(paramName, value));
                            commandText += '@' + paramName;

                            lastQuote = -1; // out of quoted value
                        }
                        else
                        {
                            lastQuote = i;  // start of quoted value
                            escapedChars = 0;
                        }

                        containsNonAscii = false;
                    }
                }
                else if (b > 0x7f && lastQuote >= 0)   // non-ascii character
                {
                    // this character may not pass:
                    containsNonAscii = true;
                    escaped = false;
                }
                else if (escaped)
                {
                    escaped = false;
                }
                else if (b == '\\') // && !escaped)
                {
                    escapedChars++;
                    escaped = true;
                }
                // handle comments (only outside quoted values):
                else if (lastQuote < 0)
                {
                    // escaped = false
                    //
                    byte bnext = ((i + 1) < query.Length) ? query[i + 1] : (byte)0;
                    if (b == '/' && bnext == '*') // /*
                    {
                        // /* comment */
                        i += 2;
                        while ((i + 1) < query.Length && (query[i] != '*' || query[i + 1] != '/'))
                            i++;    // skip comment
                    }
                    else if (   // -- or #
                        (b == '-' && bnext == '-' && (i + 2 < query.Length) && char.IsWhiteSpace((char)query[i + 2])) ||
                        (b == '#'))
                    {
                        // single line comment
                        i++;
                        while (i < query.Length && query[i] != '\n')
                            i++;
                    }
                }
            }

            //
            commandText = string.Concat(commandText, encoding.GetString(query, commandTextLast, query.Length - commandTextLast));
            return connection.ExecuteCommand(commandText, CommandType.Text, true, parameters, false);
        }
Esempio n. 2
0
 private static void UpdateConnectErrorInfo(PhpMyDbConnection connection)
 {
     var info = StaticInfo.Get;
     info.FailConnectErrorMessage = connection.GetLastErrorMessage();
     info.FailConnectErrorNumber = connection.GetLastErrorNumber();
 }
Esempio n. 3
0
 private static void UpdateConnectErrorInfo(PhpMyDbConnection connection)
 {
     failConnectErrorMessage = connection.GetLastErrorMessage();
     failConnectErrorNumber = connection.GetLastErrorNumber();
 }