Exemplo n.º 1
0
        private string GetTargetedTable(string sql)
        {
            MySqlTokenizer tokenizer = new MySqlTokenizer(sql);

            tokenizer.ReturnComments   = false;
            tokenizer.AnsiQuotes       = sql_mode.ToLowerInvariant().Contains("ansi_quotes");
            tokenizer.BackslashEscapes = !sql_mode.ToLowerInvariant().Contains("no_backslash_escapes");

            string token = null;

            while (token != "ON" || tokenizer.Quoted)
            {
                token = tokenizer.NextToken();
            }

            string tableName = tokenizer.NextToken();

            if (tokenizer.NextToken() == ".")
            {
                tableName = tokenizer.NextToken();
            }
            if (tableName.StartsWith("`"))
            {
                return(tableName.Trim('`'));
            }
            if (tableName.StartsWith("\"") && tokenizer.AnsiQuotes)
            {
                return(tableName.Trim('"'));
            }
            return(tableName);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Splits the given text containing a SQL script into individual SQL statements.
        /// </summary>
        /// <param name="sqlScript">A string containing a SQL script.</param>
        /// <returns>A list of individual SQL statements.</returns>
        public static List <string> SplitInSqlStatements(this string sqlScript)
        {
            if (string.IsNullOrEmpty(sqlScript))
            {
                return(null);
            }

            var tokenizer = new MySqlTokenizer(sqlScript.Trim());

            return(tokenizer.BreakIntoStatements());
        }
Exemplo n.º 3
0
        public static TokenInfo GetTokenInfo(string token, MySqlTokenizer tokenizer)
        {
            TokenInfo ti = new TokenInfo();

            if (tokenizer.Quoted)
            {
                if (token.StartsWith("\'"))
                {
                    ti.Type = TokenType.Literal;
                }
                else
                {
                    if (token.StartsWith("\"") && !tokenizer.AnsiQuotes)
                    {
                        ti.Type = TokenType.String;
                    }
                    else
                    {
                        ti.Type = TokenType.Identifier;
                    }
                }
            }
            else if (IsKeyword(token.ToUpperInvariant()))
            {
                ti.Type = TokenType.Keyword;
            }
            else if (tokenizer.IsComment)
            {
                ti.Type = TokenType.Comment;
            }
            else
            {
                ti.Type = TokenType.Text;
            }
            ti.Color = GetTokenColor(ti);
            return(ti);
        }
Exemplo n.º 4
0
        private static string ParseDataType(SyncRow row, MySqlTokenizer tokenizer)
        {
            StringBuilder dtd = new StringBuilder(tokenizer.NextToken().ToUpperInvariant());

            row["DATA_TYPE"] = dtd.ToString();
            string type = row["DATA_TYPE"].ToString();

            string token = tokenizer.NextToken();

            if (token == "(")
            {
                token = tokenizer.ReadParenthesis();
                dtd.AppendFormat(CultureInfo.InvariantCulture, "{0}", token);

                if (type != "ENUM" && type != "SET")
                {
                    ParseDataTypeSize(row, token);
                }
                token = tokenizer.NextToken();
            }
            else
            {
                dtd.Append(GetDataTypeDefaults(type, row));
            }

            while (token != ")" &&
                   token != "," &&
                   String.Compare(token, "begin", StringComparison.OrdinalIgnoreCase) != 0 &&
                   String.Compare(token, "return", StringComparison.OrdinalIgnoreCase) != 0)
            {
                if (String.Compare(token, "CHARACTER", StringComparison.OrdinalIgnoreCase) == 0 ||
                    String.Compare(token, "BINARY", StringComparison.OrdinalIgnoreCase) == 0)
                {
                }    // we don't need to do anything with this
                else if (String.Compare(token, "SET", StringComparison.OrdinalIgnoreCase) == 0 ||
                         String.Compare(token, "CHARSET", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    row["CHARACTER_SET_NAME"] = tokenizer.NextToken();
                }
                else if (String.Compare(token, "ASCII", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    row["CHARACTER_SET_NAME"] = "latin1";
                }
                else if (String.Compare(token, "UNICODE", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    row["CHARACTER_SET_NAME"] = "ucs2";
                }
                else if (String.Compare(token, "COLLATE", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    row["COLLATION_NAME"] = tokenizer.NextToken();
                }
                else
                {
                    dtd.AppendFormat(CultureInfo.InvariantCulture, " {0}", token);
                }
                token = tokenizer.NextToken();
            }

            if (dtd.Length > 0)
            {
                row["DTD_IDENTIFIER"] = dtd.ToString();
            }

            // now default the collation if one wasn't given
            //if (string.IsNullOrEmpty((string)row["COLLATION_NAME"]) &&
            //    !string.IsNullOrEmpty((string)row["CHARACTER_SET_NAME"]))
            //    row["COLLATION_NAME"] = CharSetMap.GetDefaultCollation(
            //        row["CHARACTER_SET_NAME"].ToString(), connection);

            // now set the octet length
            //if (row["CHARACTER_MAXIMUM_LENGTH"] != null)
            //{
            //    if (row["CHARACTER_SET_NAME"] == null)
            //        row["CHARACTER_SET_NAME"] = "";
            //    row["CHARACTER_OCTET_LENGTH"] =
            //        CharSetMap.GetMaxLength((string)row["CHARACTER_SET_NAME"], connection) *
            //        (int)row["CHARACTER_MAXIMUM_LENGTH"];
            //}

            return(token);
        }
Exemplo n.º 5
0
        private static void ParseProcedureBody(SyncTable parametersTable, string body, string sqlMode, string schema, string procName)
        {
            var modes = new List <string>(new string[3] {
                "IN", "OUT", "INOUT"
            });

            int pos       = 1;
            var tokenizer = new MySqlTokenizer(body)
            {
                AnsiQuotes       = sqlMode.IndexOf("ANSI_QUOTES") != -1,
                BackslashEscapes = sqlMode.IndexOf("NO_BACKSLASH_ESCAPES") == -1,
                ReturnComments   = false
            };
            string token = tokenizer.NextToken();

            // this block will scan for the opening paren while also determining
            // if this routine is a function.  If so, then we need to add a
            // parameter row for the return parameter since it is ordinal position
            // 0 and should appear first.
            while (token != "(")
            {
                if (String.Compare(token, "FUNCTION", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    var newRow = parametersTable.NewRow();
                    InitParameterRow(newRow, schema, procName);
                    parametersTable.Rows.Add(newRow);
                }
                token = tokenizer.NextToken();
            }
            token = tokenizer.NextToken();  // now move to the next token past the (

            while (token != ")")
            {
                var parmRow = parametersTable.NewRow();
                InitParameterRow(parmRow, schema, procName);
                parmRow["ORDINAL_POSITION"] = pos++;

                // handle mode and name for the parameter
                string mode = token.ToUpperInvariant();
                if (!tokenizer.Quoted && modes.Contains(mode))
                {
                    parmRow["PARAMETER_MODE"] = mode;
                    token = tokenizer.NextToken();
                }
                if (tokenizer.Quoted)
                {
                    token = token.Substring(1, token.Length - 2);
                }
                parmRow["PARAMETER_NAME"] = token;

                // now parse data type
                token = ParseDataType(parmRow, tokenizer);
                if (token == ",")
                {
                    token = tokenizer.NextToken();
                }

                parametersTable.Rows.Add(parmRow);
            }

            // now parse out the return parameter if there is one.
            token = tokenizer.NextToken().ToUpperInvariant();

            if (String.Compare(token, "RETURNS", StringComparison.OrdinalIgnoreCase) == 0)
            {
                var parameterRow = parametersTable.Rows[0];
                parameterRow["PARAMETER_NAME"] = "RETURN_VALUE";
                ParseDataType(parameterRow, tokenizer);
            }
        }
Exemplo n.º 6
0
 public static TokenInfo GetTokenInfo(string token, MySqlTokenizer tokenizer)
 {
     TokenInfo ti = new TokenInfo();
     if (tokenizer.Quoted)
     {
         if (token.StartsWith("\'"))
             ti.Type = TokenType.Literal;
         else
         {
             if (token.StartsWith("\"") && !tokenizer.AnsiQuotes)
                 ti.Type = TokenType.String;
             else
                 ti.Type = TokenType.Identifier;
         }
     }
     else if (IsKeyword(token.ToUpperInvariant()))
         ti.Type = TokenType.Keyword;
     else if (tokenizer.IsComment)
         ti.Type = TokenType.Comment;
     else
         ti.Type = TokenType.Text;
     ti.Color = GetTokenColor(ti);
     return ti;
 }