Пример #1
0
        /// <summary>
        /// Parses the isql statement SET AUTODDL and sets the character set to current connection string.
        /// </summary>
        /// <param name="setAutoDdlStatement">The set names statement.</param>
        protected void SetAutoDdl(string setAutoDdlStatement, ref bool autoCommit)
        {
            // SET AUTODDL [ON | OFF]
            StringParser parser = new StringParser(setAutoDdlStatement, false);

            parser.Tokens = new[] { " ", "\r\n", "\n", "\r" };
            parser.ParseNext();
            if (parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture) != "SET")
            {
                throw new ArgumentException("Malformed isql SET statement. Expected keyword SET but something else was found.");
            }
            parser.ParseNext();             // AUTO
            if (parser.ParseNext() != -1)
            {
                string onOff = parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture);
                if (onOff == "ON")
                {
                    autoCommit = true;
                }
                else if (onOff == "OFF")
                {
                    autoCommit = false;
                }
                else
                {
                    throw new ArgumentException("Expected the ON or OFF but something else was found.");
                }
            }
            else
            {
                autoCommit = !autoCommit;
            }
        }
Пример #2
0
        /// <summary>
        /// Parses the isql statement SET NAMES and sets the character set to current connection string.
        /// </summary>
        /// <param name="setNamesStatement">The set names statement.</param>
        protected void SetNames(string setNamesStatement)
        {
            // SET NAMES charset
            StringParser parser = new StringParser(setNamesStatement, false);

            parser.Tokens = new[] { " ", "\r\n", "\n", "\r" };
            parser.ParseNext();
            if (parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture) != "SET")
            {
                throw new ArgumentException("Malformed isql SET statement. Expected keyword SET but something else was found.");
            }
            parser.ParseNext();             // NAMES
            parser.ParseNext();
            this.connectionString.Charset = parser.Result;
        }
        /// <summary>
        /// Parses the SQL code and loads the SQL statements into the StringCollection <see cref="Results"/>.
        /// </summary>
        /// <returns>The number of statements found.</returns>
        public int Parse()
        {
            int    index = 0;
            string atomicResult;
            string newParserToken;

            _results.Clear();

            while (index < _parser.Length)
            {
                index        = _parser.ParseNext();
                atomicResult = _parser.Result.Trim();

                if (IsSetTermStatement(atomicResult, out newParserToken))
                {
                    _parser.Tokens = new[] { newParserToken };
                    continue;
                }

                if (atomicResult != null && atomicResult.Length > 0)
                {
                    _results.Add(atomicResult);
                }
            }

            return(_results.Count);
        }
Пример #4
0
        /// <summary>
        /// Parses the isql statement SET SQL DIALECT and sets the dialect set to current connection string.
        /// </summary>
        /// <param name="setSqlDialectStatement">The set sql dialect statement.</param>
        protected void SetSqlDialect(string setSqlDialectStatement)
        {
            // SET SQL DIALECT dialect
            StringParser parser = new StringParser(setSqlDialectStatement, false);

            parser.Tokens = new[] { " ", "\r\n", "\n", "\r" };
            parser.ParseNext();
            if (parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture) != "SET")
            {
                throw new ArgumentException("Malformed isql SET statement. Expected keyword SET but something else was found.");
            }
            parser.ParseNext();             // SQL
            parser.ParseNext();             // DIALECT
            parser.ParseNext();
            int dialect = 3;

            int.TryParse(parser.Result, out dialect);
            this.connectionString.Dialect = dialect;
        }
Пример #5
0
        /// <summary>
        /// Updates the connection string with the data parsed from the parameter and opens a connection
        /// to the database.
        /// </summary>
        /// <param name="connectDbStatement"></param>
        protected internal void ConnectToDatabase(string connectDbStatement)
        {
            // CONNECT 'filespec'
            // [USER 'username']
            // [PASSWORD 'password']
            // [CACHE int]
            // [ROLE 'rolename']
            StringParser parser = new StringParser(connectDbStatement, false);

            parser.Tokens = new[] { " ", "\r\n", "\n", "\r" };
            parser.ParseNext();
            if (parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture) != "CONNECT")
            {
                throw new ArgumentException("Malformed isql CONNECT statement. Expected keyword CONNECT but something else was found.");
            }
            parser.ParseNext();
            this.connectionString.Database = parser.Result.Replace("'", string.Empty);
            while (parser.ParseNext() != -1)
            {
                switch (parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture))
                {
                case "USER":
                    parser.ParseNext();
                    this.connectionString.UserID = parser.Result.Replace("'", string.Empty);
                    break;

                case "PASSWORD":
                    parser.ParseNext();
                    this.connectionString.Password = parser.Result.Replace("'", string.Empty);
                    break;

                case "CACHE":
                    parser.ParseNext();
                    break;

                case "ROLE":
                    parser.ParseNext();
                    this.connectionString.Role = parser.Result.Replace("'", string.Empty);
                    break;

                default:
                    throw new ArgumentException("Unexpected token '" + parser.Result.Trim() + "' on isql CONNECT statement.");
                }
            }
            this.requiresNewConnection = true;
            this.ProvideConnection();
        }
        /// <summary>
        /// Parses the isql statement CREATE DATABASE and creates the database and opens a connection to the recently created database.
        /// </summary>
        /// <param name="createDbStatement">the create database statement.</param>
        protected internal void CreateDatabase(string createDbStatement)
        {
            // CREATE {DATABASE | SCHEMA} 'filespec'
            // [USER 'username' [PASSWORD 'password']]
            // [PAGE_SIZE [=] int]
            // [LENGTH [=] int [PAGE[S]]]
            // [DEFAULT CHARACTER SET charset]
            // [<secondary_file>];	
            int pageSize = 0;
            StringParser parser = new StringParser(createDbStatement, false);
            parser.Token = " ";
            parser.ParseNext();
            if (parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture) != "CREATE")
            {
                throw new Exception("Malformed isql CREATE statement. Expected keyword CREATE but something else was found.");
            }
            parser.ParseNext(); // {DATABASE | SCHEMA}
            parser.ParseNext();
            this.connectionString.Database = parser.Result.Replace("'", string.Empty);
            while (parser.ParseNext() != -1)
            {
                switch (parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture))
                {
                    case "USER":
                        parser.ParseNext();
                        this.connectionString.UserID = parser.Result.Replace("'", string.Empty);
                        break;

                    case "PASSWORD":
                        parser.ParseNext();
                        this.connectionString.Password = parser.Result.Replace("'", string.Empty);
                        break;

                    case "PAGE_SIZE":
                        parser.ParseNext();
                        if (parser.Result.Trim() == "=")
                            parser.ParseNext();
                        int.TryParse(parser.Result, out pageSize);
                        break;

                    case "DEFAULT":
                        parser.ParseNext();
                        if (parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture) != "CHARACTER")
                            throw new Exception("Expected the keyword CHARACTER but something else was found.");

                        parser.ParseNext();
                        if (parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture) != "SET")
                            throw new Exception("Expected the keyword SET but something else was found.");

                        parser.ParseNext();
                        this.connectionString.Charset = parser.Result;
                        break;
                }
            }
            FbConnection.CreateDatabase(this.connectionString.ToString(), pageSize, true, false);
            this.requiresNewConnection = true;
            this.ProvideConnection();
        }
        /// <summary>
        /// Updates the connection string with the data parsed from the parameter and opens a connection
        /// to the database.
        /// </summary>
        /// <param name="connectDbStatement"></param>
        protected internal void ConnectToDatabase(string connectDbStatement)
        {
            // CONNECT 'filespec' [USER 'username'][PASSWORD 'password'] [CACHE int] [ROLE 'rolename']
            StringParser parser = new StringParser(connectDbStatement, false);
            parser.Token = " ";
            parser.ParseNext();
            if (parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture) != "CONNECT")
            {
                throw new Exception("Malformed isql CONNECT statement. Expected keyword CONNECT but something else was found.");
            }
            parser.ParseNext();
            this.connectionString.Database = parser.Result.Replace("'", string.Empty);
            while (parser.ParseNext() != -1)
            {
                switch (parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture))
                {
                    case "USER":
                        parser.ParseNext();
                        this.connectionString.UserID = parser.Result.Replace("'", string.Empty);
                        break;

                    case "PASSWORD":
                        parser.ParseNext();
                        this.connectionString.Password = parser.Result.Replace("'", string.Empty);
                        break;

                    case "CACHE":
                        parser.ParseNext();
                        break;

                    case "ROLE":
                        parser.ParseNext();
                        this.connectionString.Role = parser.Result.Replace("'", string.Empty);
                        break;

                    default:
                        throw new Exception("Unexpected token '" + parser.Result.Trim() + "' on isql CONNECT statement.");

                }
            }
            this.requiresNewConnection = true;
            this.ProvideConnection();
        }
Пример #8
0
        /// <summary>
        /// Parses the isql statement CREATE DATABASE and creates the database and opens a connection to the recently created database.
        /// </summary>
        /// <param name="createDbStatement">the create database statement.</param>
        protected internal void CreateDatabase(string createDbStatement)
        {
            // CREATE {DATABASE | SCHEMA} 'filespec'
            // [USER 'username' [PASSWORD 'password']]
            // [PAGE_SIZE [=] int]
            // [LENGTH [=] int [PAGE[S]]]
            // [DEFAULT CHARACTER SET charset]
            // [<secondary_file>];
            int          pageSize = 0;
            StringParser parser   = new StringParser(createDbStatement, false);

            parser.Token = " ";
            parser.ParseNext();
            if (parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture) != "CREATE")
            {
                throw new Exception("Malformed isql CREATE statement. Expected keyword CREATE but something else was found.");
            }
            parser.ParseNext();             // {DATABASE | SCHEMA}
            parser.ParseNext();
            this.connectionString.Database = parser.Result.Replace("'", string.Empty);
            while (parser.ParseNext() != -1)
            {
                switch (parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture))
                {
                case "USER":
                    parser.ParseNext();
                    this.connectionString.UserID = parser.Result.Replace("'", string.Empty);
                    break;

                case "PASSWORD":
                    parser.ParseNext();
                    this.connectionString.Password = parser.Result.Replace("'", string.Empty);
                    break;

                case "PAGE_SIZE":
                    parser.ParseNext();
                    if (parser.Result.Trim() == "=")
                    {
                        parser.ParseNext();
                    }
                    int.TryParse(parser.Result, out pageSize);
                    break;

                case "DEFAULT":
                    parser.ParseNext();
                    if (parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture) != "CHARACTER")
                    {
                        throw new Exception("Expected the keyword CHARACTER but something else was found.");
                    }

                    parser.ParseNext();
                    if (parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture) != "SET")
                    {
                        throw new Exception("Expected the keyword SET but something else was found.");
                    }

                    parser.ParseNext();
                    this.connectionString.Charset = parser.Result;
                    break;
                }
            }
            FbConnection.CreateDatabase(this.connectionString.ToString(), pageSize, true, false);
            this.requiresNewConnection = true;
            this.ProvideConnection();
        }
		/// <summary>
		/// Parses the isql statement SET SQL DIALECT and sets the dialect set to current connection string.
		/// </summary>
		/// <param name="setSqlDialectStatement">The set sql dialect statement.</param>
		protected void SetSqlDialect(string setSqlDialectStatement)
		{
			// SET SQL DIALECT dialect
			StringParser parser = new StringParser(setSqlDialectStatement);
			parser.Tokens = new[] { " ", "\r\n", "\n", "\r" };
			parser.ParseNext();
			if (parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture) != "SET")
			{
				throw new ArgumentException("Malformed isql SET statement. Expected keyword SET but something else was found.");
			}
			parser.ParseNext(); // SQL
			parser.ParseNext(); // DIALECT
			parser.ParseNext();
			int dialect = 3;
			int.TryParse(parser.Result, out dialect);
			_connectionString.Dialect = dialect;
		}
		/// <summary>
		/// Parses the isql statement SET NAMES and sets the character set to current connection string.
		/// </summary>
		/// <param name="setNamesStatement">The set names statement.</param>
		protected void SetNames(string setNamesStatement)
		{
			// SET NAMES charset
			StringParser parser = new StringParser(setNamesStatement);
			parser.Tokens = new[] { " ", "\r\n", "\n", "\r" };
			parser.ParseNext();
			if (parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture) != "SET")
			{
				throw new ArgumentException("Malformed isql SET statement. Expected keyword SET but something else was found.");
			}
			parser.ParseNext(); // NAMES
			parser.ParseNext();
			_connectionString.Charset = parser.Result;
		}
		/// <summary>
		/// Parses the isql statement SET AUTODDL and sets the character set to current connection string.
		/// </summary>
		/// <param name="setAutoDdlStatement">The set names statement.</param>
		protected void SetAutoDdl(string setAutoDdlStatement, ref bool autoCommit)
		{
			// SET AUTODDL [ON | OFF]
			StringParser parser = new StringParser(setAutoDdlStatement);
			parser.Tokens = new[] { " ", "\r\n", "\n", "\r" };
			parser.ParseNext();
			if (parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture) != "SET")
			{
				throw new ArgumentException("Malformed isql SET statement. Expected keyword SET but something else was found.");
			}
			parser.ParseNext(); // AUTO
			if (parser.ParseNext() != -1)
			{
				string onOff = parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture);
				if (onOff == "ON")
				{
					autoCommit = true;
				}
				else if (onOff == "OFF")
				{
					autoCommit = false;
				}
				else
				{
					throw new ArgumentException("Expected the ON or OFF but something else was found.");
				}
			}
			else
			{
				autoCommit = !autoCommit;
			}
		}