/// <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();
        }
		public FbScript(string script)
		{
			this.results = new FbStatementCollection();
			this.parser = new StringParser(RemoveComments(script), false);
			this.parser.Tokens = new[] { ";" };
		}
        /// <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();
        }
		/// <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;
			}
		}
Example #7
0
        /// <summary>
        /// Determines the <see cref="SqlStatementType"/> of the provided SQL statement.
        /// </summary>
        /// <param name="sqlStatement">The string containing the SQL statement.</param>
        /// <returns>The <see cref="SqlStatementType"/> of the <b>sqlStatement</b>.</returns>
        /// <remarks>If the type of <b>sqlStatement</b> could not be determined this
        /// method will throw an exception.</remarks>
        public static SqlStatementType GetStatementType(string sqlStatement)
        {
            char type = sqlStatement == null ? ' ' : sqlStatement.Trim().ToUpper(CultureInfo.CurrentUICulture)[0];

            switch (type)
            {
            case 'A':
                if (StringParser.StartsWith(sqlStatement, "ALTER DATABASE", true))
                {
                    return(SqlStatementType.AlterDatabase);
                }
                if (StringParser.StartsWith(sqlStatement, "ALTER DOMAIN", true))
                {
                    return(SqlStatementType.AlterDomain);
                }
                if (StringParser.StartsWith(sqlStatement, "ALTER EXCEPTION", true))
                {
                    return(SqlStatementType.AlterException);
                }
                if (StringParser.StartsWith(sqlStatement, "ALTER INDEX", true))
                {
                    return(SqlStatementType.AlterIndex);
                }
                if (StringParser.StartsWith(sqlStatement, "ALTER PROCEDURE", true))
                {
                    return(SqlStatementType.AlterProcedure);
                }
                if (StringParser.StartsWith(sqlStatement, "ALTER SEQUENCE", true))
                {
                    return(SqlStatementType.AlterSequence);
                }
                if (StringParser.StartsWith(sqlStatement, "ALTER TABLE", true))
                {
                    return(SqlStatementType.AlterTable);
                }
                if (StringParser.StartsWith(sqlStatement, "ALTER TRIGGER", true))
                {
                    return(SqlStatementType.AlterTrigger);
                }
                if (StringParser.StartsWith(sqlStatement, "ALTER VIEW", true))
                {
                    return(SqlStatementType.AlterView);
                }
                break;

            case 'C':
                switch (char.ToUpper(sqlStatement[1], CultureInfo.CurrentUICulture))
                {
                case 'L':
                    if (StringParser.StartsWith(sqlStatement, "CLOSE", true))
                    {
                        return(SqlStatementType.Close);
                    }
                    break;

                case 'O':
                    if (StringParser.StartsWith(sqlStatement, "COMMENT ON", true))
                    {
                        return(SqlStatementType.CommentOn);
                    }
                    if (StringParser.StartsWith(sqlStatement, "COMMIT", true))
                    {
                        return(SqlStatementType.Commit);
                    }
                    if (StringParser.StartsWith(sqlStatement, "CONNECT", true))
                    {
                        return(SqlStatementType.Connect);
                    }
                    break;

                case 'R':
                    if (StringParser.StartsWith(sqlStatement, "CREATE DATABASE", true))
                    {
                        return(SqlStatementType.CreateDatabase);
                    }
                    if (StringParser.StartsWith(sqlStatement, "CREATE DOMAIN", true))
                    {
                        return(SqlStatementType.CreateDomain);
                    }
                    if (StringParser.StartsWith(sqlStatement, "CREATE EXCEPTION", true) ||
                        StringParser.StartsWith(sqlStatement, "CREATE OR ALTER EXCEPTION", true))
                    {
                        return(SqlStatementType.CreateException);
                    }
                    if (StringParser.StartsWith(sqlStatement, "CREATE GENERATOR", true))
                    {
                        return(SqlStatementType.CreateGenerator);
                    }
                    if (StringParser.StartsWith(sqlStatement, "CREATE INDEX", true) ||
                        StringParser.StartsWith(sqlStatement, "CREATE ASC INDEX", true) ||
                        StringParser.StartsWith(sqlStatement, "CREATE ASCENDING INDEX", true) ||
                        StringParser.StartsWith(sqlStatement, "CREATE DESC INDEX", true) ||
                        StringParser.StartsWith(sqlStatement, "CREATE DESCENDING INDEX", true))
                    {
                        return(SqlStatementType.CreateIndex);
                    }
                    if (StringParser.StartsWith(sqlStatement, "CREATE PROCEDURE", true) ||
                        StringParser.StartsWith(sqlStatement, "CREATE OR ALTER PROCEDURE", true))
                    {
                        return(SqlStatementType.CreateProcedure);
                    }
                    if (StringParser.StartsWith(sqlStatement, "CREATE ROLE", true))
                    {
                        return(SqlStatementType.CreateRole);
                    }
                    if (StringParser.StartsWith(sqlStatement, "CREATE SEQUENCE", true))
                    {
                        return(SqlStatementType.CreateSequence);
                    }
                    if (StringParser.StartsWith(sqlStatement, "CREATE SHADOW", true))
                    {
                        return(SqlStatementType.CreateShadow);
                    }
                    if (StringParser.StartsWith(sqlStatement, "CREATE TABLE", true) ||
                        StringParser.StartsWith(sqlStatement, "CREATE GLOBAL TEMPORARY TABLE", true))
                    {
                        return(SqlStatementType.CreateTable);
                    }
                    if (StringParser.StartsWith(sqlStatement, "CREATE TRIGGER", true) ||
                        StringParser.StartsWith(sqlStatement, "CREATE OR ALTER TRIGGER", true))
                    {
                        return(SqlStatementType.CreateTrigger);
                    }
                    if (StringParser.StartsWith(sqlStatement, "CREATE UNIQUE INDEX", true) ||
                        StringParser.StartsWith(sqlStatement, "CREATE UNIQUE ASC INDEX", true) ||
                        StringParser.StartsWith(sqlStatement, "CREATE UNIQUE ASCENDING INDEX", true) ||
                        StringParser.StartsWith(sqlStatement, "CREATE UNIQUE DESC INDEX", true) ||
                        StringParser.StartsWith(sqlStatement, "CREATE UNIQUE DESCENDING INDEX", true))
                    {
                        return(SqlStatementType.CreateIndex);
                    }
                    if (StringParser.StartsWith(sqlStatement, "CREATE VIEW", true) ||
                        StringParser.StartsWith(sqlStatement, "CREATE OR ALTER VIEW", true))
                    {
                        return(SqlStatementType.CreateView);
                    }
                    break;
                }
                break;

            case 'D':
                switch (char.ToUpper(sqlStatement[1], CultureInfo.CurrentUICulture))
                {
                case 'E':
                    if (StringParser.StartsWith(sqlStatement, "DECLARE CURSOR", true))
                    {
                        return(SqlStatementType.DeclareCursor);
                    }
                    if (StringParser.StartsWith(sqlStatement, "DECLARE EXTERNAL FUNCTION", true))
                    {
                        return(SqlStatementType.DeclareExternalFunction);
                    }
                    if (StringParser.StartsWith(sqlStatement, "DECLARE FILTER", true))
                    {
                        return(SqlStatementType.DeclareFilter);
                    }
                    if (StringParser.StartsWith(sqlStatement, "DECLARE STATEMENT", true))
                    {
                        return(SqlStatementType.DeclareStatement);
                    }
                    if (StringParser.StartsWith(sqlStatement, "DECLARE TABLE", true))
                    {
                        return(SqlStatementType.DeclareTable);
                    }
                    if (StringParser.StartsWith(sqlStatement, "DELETE", true))
                    {
                        return(SqlStatementType.Delete);
                    }
                    if (StringParser.StartsWith(sqlStatement, "DESCRIBE", true))
                    {
                        return(SqlStatementType.Describe);
                    }
                    break;

                case 'I':
                    if (StringParser.StartsWith(sqlStatement, "DISCONNECT", true))
                    {
                        return(SqlStatementType.Disconnect);
                    }
                    break;

                case 'R':
                    if (StringParser.StartsWith(sqlStatement, "DROP DATABASE", true))
                    {
                        return(SqlStatementType.DropDatabase);
                    }
                    if (StringParser.StartsWith(sqlStatement, "DROP DOMAIN", true))
                    {
                        return(SqlStatementType.DropDomain);
                    }
                    if (StringParser.StartsWith(sqlStatement, "DROP EXCEPTION", true))
                    {
                        return(SqlStatementType.DropException);
                    }
                    if (StringParser.StartsWith(sqlStatement, "DROP EXTERNAL FUNCTION", true))
                    {
                        return(SqlStatementType.DropExternalFunction);
                    }
                    if (StringParser.StartsWith(sqlStatement, "DROP FILTER", true))
                    {
                        return(SqlStatementType.DropFilter);
                    }
                    if (StringParser.StartsWith(sqlStatement, "DROP GENERATOR", true))
                    {
                        return(SqlStatementType.DropGenerator);
                    }
                    if (StringParser.StartsWith(sqlStatement, "DROP INDEX", true))
                    {
                        return(SqlStatementType.DropIndex);
                    }
                    if (StringParser.StartsWith(sqlStatement, "DROP PROCEDURE", true))
                    {
                        return(SqlStatementType.DropProcedure);
                    }
                    if (StringParser.StartsWith(sqlStatement, "DROP SEQUENCE", true))
                    {
                        return(SqlStatementType.DropSequence);
                    }
                    if (StringParser.StartsWith(sqlStatement, "DROP ROLE", true))
                    {
                        return(SqlStatementType.DropRole);
                    }
                    if (StringParser.StartsWith(sqlStatement, "DROP SHADOW", true))
                    {
                        return(SqlStatementType.DropShadow);
                    }
                    if (StringParser.StartsWith(sqlStatement, "DROP TABLE", true))
                    {
                        return(SqlStatementType.DropTable);
                    }
                    if (StringParser.StartsWith(sqlStatement, "DROP TRIGGER", true))
                    {
                        return(SqlStatementType.DropTrigger);
                    }
                    if (StringParser.StartsWith(sqlStatement, "DROP VIEW", true))
                    {
                        return(SqlStatementType.DropView);
                    }
                    break;
                }
                break;

            case 'E':
                if (StringParser.StartsWith(sqlStatement, "EXECUTE PROCEDURE", true))
                {
                    return(SqlStatementType.ExecuteProcedure);
                }
                if (StringParser.StartsWith(sqlStatement, "EXECUTE IMMEDIATE", true))
                {
                    return(SqlStatementType.ExecuteImmediate);
                }
                if (StringParser.StartsWith(sqlStatement, "EXECUTE", true))
                {
                    return(SqlStatementType.Execute);
                }
                if (StringParser.StartsWith(sqlStatement, "EVENT WAIT", true))
                {
                    return(SqlStatementType.EventWait);
                }
                if (StringParser.StartsWith(sqlStatement, "EVENT INIT", true))
                {
                    return(SqlStatementType.EventInit);
                }
                if (StringParser.StartsWith(sqlStatement, "END DECLARE SECTION", true))
                {
                    return(SqlStatementType.EndDeclareSection);
                }
                break;

            case 'F':
                if (StringParser.StartsWith(sqlStatement, "FETCH", true))
                {
                    return(SqlStatementType.Fetch);
                }
                break;

            case 'G':
                if (StringParser.StartsWith(sqlStatement, "GRANT", true))
                {
                    return(SqlStatementType.Grant);
                }
                break;

            case 'I':
                if (StringParser.StartsWith(sqlStatement, "INSERT CURSOR", true))
                {
                    return(SqlStatementType.InsertCursor);
                }
                if (StringParser.StartsWith(sqlStatement, "INSERT", true))
                {
                    return(SqlStatementType.Insert);
                }
                break;

            case 'O':
                if (StringParser.StartsWith(sqlStatement, "OPEN", true))
                {
                    return(SqlStatementType.Open);
                }
                break;

            case 'P':
                if (StringParser.StartsWith(sqlStatement, "PREPARE", true))
                {
                    return(SqlStatementType.Prepare);
                }
                break;

            case 'R':
                if (StringParser.StartsWith(sqlStatement, "REVOKE", true))
                {
                    return(SqlStatementType.Revoke);
                }
                if (StringParser.StartsWith(sqlStatement, "RECREATE PROCEDURE", true))
                {
                    return(SqlStatementType.RecreateProcedure);
                }
                if (StringParser.StartsWith(sqlStatement, "RECREATE TABLE", true) ||
                    StringParser.StartsWith(sqlStatement, "RECREATE GLOBAL TEMPORARY TABLE", true))
                {
                    return(SqlStatementType.RecreateTable);
                }
                if (StringParser.StartsWith(sqlStatement, "RECREATE TRIGGER", true))
                {
                    return(SqlStatementType.RecreateTrigger);
                }
                if (StringParser.StartsWith(sqlStatement, "RECREATE VIEW", true))
                {
                    return(SqlStatementType.RecreateView);
                }
                if (StringParser.StartsWith(sqlStatement, "ROLLBACK", true))
                {
                    return(SqlStatementType.Rollback);
                }
                break;

            case 'S':
                if (StringParser.StartsWith(sqlStatement, "SELECT", true))
                {
                    return(SqlStatementType.Select);
                }
                if (StringParser.StartsWith(sqlStatement, "SET AUTODDL", true))
                {
                    return(SqlStatementType.SetAutoDDL);
                }
                if (StringParser.StartsWith(sqlStatement, "SET DATABASE", true))
                {
                    return(SqlStatementType.SetDatabase);
                }
                if (StringParser.StartsWith(sqlStatement, "SET GENERATOR", true))
                {
                    return(SqlStatementType.SetGenerator);
                }
                if (StringParser.StartsWith(sqlStatement, "SET NAMES", true))
                {
                    return(SqlStatementType.SetNames);
                }
                if (StringParser.StartsWith(sqlStatement, "SET SQL DIALECT", true))
                {
                    return(SqlStatementType.SetSQLDialect);
                }
                if (StringParser.StartsWith(sqlStatement, "SET STATISTICS", true))
                {
                    return(SqlStatementType.SetStatistics);
                }
                if (StringParser.StartsWith(sqlStatement, "SET TRANSACTION", true))
                {
                    return(SqlStatementType.SetTransaction);
                }
                if (StringParser.StartsWith(sqlStatement, "SHOW SQL DIALECT", true))
                {
                    return(SqlStatementType.ShowSQLDialect);
                }
                break;

            case 'U':
                if (StringParser.StartsWith(sqlStatement, "UPDATE", true))
                {
                    return(SqlStatementType.Update);
                }
                break;

            case 'W':
                if (StringParser.StartsWith(sqlStatement, "WHENEVER", true))
                {
                    return(SqlStatementType.Whenever);
                }
                break;
            }
            throw new ArgumentException("The type of the SQL statement could not be determined.");
        }
Example #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="createDatabaseStatement">The create database statement.</param>
        protected void CreateDatabase(string createDatabaseStatement)
        {
            // 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(createDatabaseStatement, false);

            parser.Tokens = new[] { " ", "\r\n", "\n", "\r" };
            parser.ParseNext();
            if (parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture) != "CREATE")
            {
                throw new ArgumentException("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 ArgumentException("Expected the keyword CHARACTER but something else was found.");
                    }

                    parser.ParseNext();
                    if (parser.Result.Trim().ToUpper(CultureInfo.CurrentUICulture) != "SET")
                    {
                        throw new ArgumentException("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();
        }