Example #1
0
        private void ProcessCreateTable(Tokenizer tokenizer, Channel channel, bool cached)
        {
            Table  t;
            string sToken = tokenizer.GetName();

            if (cached && _log != null)
            {
                t = new Table(this, true, sToken, true);
            }
            else
            {
                t = new Table(this, true, sToken, false);
            }

            tokenizer.GetThis("(");

            int  primarykeycolumn = -1;
            int  column           = 0;
            bool constraint       = false;

            while (true)
            {
                bool identity = false;

                sToken = tokenizer.GetString();

                if (sToken.Equals("CONSTRAINT") || sToken.Equals("PRIMARY") ||
                    sToken.Equals("FOREIGN") || sToken.Equals("UNIQUE"))
                {
                    tokenizer.Back();

                    constraint = true;

                    break;
                }

                string     sColumn = sToken;
                ColumnType iType   = Column.GetColumnType(tokenizer.GetString());

                if (iType == ColumnType.VarChar && _ignoreCase)
                {
                    iType = ColumnType.VarCharIgnoreCase;
                }

                sToken = tokenizer.GetString();

                if (iType == ColumnType.DbDouble && sToken.Equals("PRECISION"))
                {
                    sToken = tokenizer.GetString();
                }

                if (sToken.Equals("("))
                {
                    // overread length
                    do
                    {
                        sToken = tokenizer.GetString();
                    } while (!sToken.Equals(")"));

                    sToken = tokenizer.GetString();
                }

                bool nullable = true;

                if (sToken.Equals("NULL"))
                {
                    sToken = tokenizer.GetString();
                }
                else if (sToken.Equals("NOT"))
                {
                    tokenizer.GetThis("NULL");

                    nullable = false;
                    sToken   = tokenizer.GetString();
                }

                if (sToken.Equals("IDENTITY"))
                {
                    identity = true;

                    Trace.Check(primarykeycolumn == -1, Trace.SECOND_PRIMARY_KEY,
                                sColumn);

                    sToken           = tokenizer.GetString();
                    primarykeycolumn = column;
                }

                if (sToken.Equals("PRIMARY"))
                {
                    tokenizer.GetThis("KEY");
                    Trace.Check(identity || primarykeycolumn == -1,
                                Trace.SECOND_PRIMARY_KEY, sColumn);

                    primarykeycolumn = column;
                    sToken           = tokenizer.GetString();
                }

                t.AddColumn(sColumn, iType, nullable, identity);

                if (sToken.Equals(")"))
                {
                    break;
                }

                if (!sToken.Equals(","))
                {
                    throw Trace.Error(Trace.UnexpectedToken, sToken);
                }

                column++;
            }

            if (primarykeycolumn != -1)
            {
                t.CreatePrimaryKey(primarykeycolumn);
            }
            else
            {
                t.CreatePrimaryKey();
            }

            if (constraint)
            {
                int i = 0;

                while (true)
                {
                    sToken = tokenizer.GetString();

                    string name = "SYSTEM_CONSTRAINT" + i;

                    i++;

                    if (sToken.Equals("CONSTRAINT"))
                    {
                        name   = tokenizer.GetString();
                        sToken = tokenizer.GetString();
                    }

                    if (sToken.Equals("PRIMARY"))
                    {
                        tokenizer.GetThis("KEY");
                        AddUniqueConstraintOn(tokenizer, channel, name, t);
                    }
                    else if (sToken.Equals("UNIQUE"))
                    {
                        AddUniqueConstraintOn(tokenizer, channel, name, t);
                    }
                    else if (sToken.Equals("FOREIGN"))
                    {
                        tokenizer.GetThis("KEY");
                        AddForeignKeyOn(tokenizer, channel, name, t);
                    }

                    sToken = tokenizer.GetString();

                    if (sToken.Equals(")"))
                    {
                        break;
                    }

                    if (!sToken.Equals(","))
                    {
                        throw Trace.Error(Trace.UnexpectedToken, sToken);
                    }
                }
            }

            channel.Commit();
            LinkTable(t);
        }
Example #2
0
        private void ProcessCreateTable(Tokenizer tokenizer, Channel channel, bool cached)
        {
            Table  t;
            string sToken = tokenizer.GetName();

            if (cached && _log != null)
            {
                t = new Table(this, true, sToken, true);
            }
            else
            {
                t = new Table(this, true, sToken, false);
            }

            tokenizer.GetThis("(");

            int     primarykeycolumn = -1;
            int     column = 0;
            bool constraint = false;

            while (true)
            {
                bool identity = false;

                sToken = tokenizer.GetString();

                if (sToken.Equals("CONSTRAINT") || sToken.Equals("PRIMARY")
                    || sToken.Equals("FOREIGN") || sToken.Equals("UNIQUE"))
                {
                    tokenizer.Back();

                    constraint = true;

                    break;
                }

                string sColumn = sToken;
                ColumnType iType = Column.GetColumnType(tokenizer.GetString());

                if (iType == ColumnType.VarChar && _ignoreCase)
                {
                    iType = ColumnType.VarCharIgnoreCase;
                }

                sToken = tokenizer.GetString();

                if (iType == ColumnType.DbDouble && sToken.Equals("PRECISION"))
                {
                    sToken = tokenizer.GetString();
                }

                if (sToken.Equals("("))
                {

                    // overread length
                    do
                    {
                        sToken = tokenizer.GetString();
                    } while (!sToken.Equals(")"));

                    sToken = tokenizer.GetString();
                }

                bool nullable = true;

                if (sToken.Equals("NULL"))
                {
                    sToken = tokenizer.GetString();
                }
                else if (sToken.Equals("NOT"))
                {
                    tokenizer.GetThis("NULL");

                    nullable = false;
                    sToken = tokenizer.GetString();
                }

                if (sToken.Equals("IDENTITY"))
                {
                    identity = true;

                    TracingHelper.Check(primarykeycolumn == -1, TracingHelper.SECOND_PRIMARY_KEY,
                        sColumn);

                    sToken = tokenizer.GetString();
                    primarykeycolumn = column;
                }

                if (sToken.Equals("PRIMARY"))
                {
                    tokenizer.GetThis("KEY");
                    TracingHelper.Check(identity || primarykeycolumn == -1,
                        TracingHelper.SECOND_PRIMARY_KEY, sColumn);

                    primarykeycolumn = column;
                    sToken = tokenizer.GetString();
                }

                t.AddColumn(sColumn, iType, nullable, identity);

                if (sToken.Equals(")"))
                {
                    break;
                }

                if (!sToken.Equals(","))
                {
                    throw TracingHelper.Error(TracingHelper.UnexpectedToken, sToken);
                }

                column++;
            }

            if (primarykeycolumn != -1)
            {
                t.CreatePrimaryKey(primarykeycolumn);
            }
            else
            {
                t.CreatePrimaryKey();
            }

            if (constraint)
            {
                int i = 0;

                while (true)
                {
                    sToken = tokenizer.GetString();

                    string name = "SYSTEM_CONSTRAINT" + i;

                    i++;

                    if (sToken.Equals("CONSTRAINT"))
                    {
                        name = tokenizer.GetString();
                        sToken = tokenizer.GetString();
                    }

                    if (sToken.Equals("PRIMARY"))
                    {
                        tokenizer.GetThis("KEY");
                        AddUniqueConstraintOn(tokenizer, channel, name, t);
                    }
                    else if (sToken.Equals("UNIQUE"))
                    {
                        AddUniqueConstraintOn(tokenizer, channel, name, t);
                    }
                    else if (sToken.Equals("FOREIGN"))
                    {
                        tokenizer.GetThis("KEY");
                        AddForeignKeyOn(tokenizer, channel, name, t);
                    }

                    sToken = tokenizer.GetString();

                    if (sToken.Equals(")"))
                    {
                        break;
                    }

                    if (!sToken.Equals(","))
                    {
                        throw TracingHelper.Error(TracingHelper.UnexpectedToken, sToken);
                    }
                }
            }

            channel.Commit();
            LinkTable(t);
        }
Example #3
0
        private Result ProcessCreate(Tokenizer tokenizer, Channel channel)
        {
            channel.CheckReadWrite();
            channel.CheckAdmin();

            string sToken = tokenizer.GetString();

            switch (sToken)
            {
            case "TABLE":
                ProcessCreateTable(tokenizer, channel, true);
                break;

            case "MEMORY":
                tokenizer.GetThis("TABLE");
                ProcessCreateTable(tokenizer, channel, false);
                break;

            case "CACHED":
                tokenizer.GetThis("TABLE");
                ProcessCreateTable(tokenizer, channel, true);
                break;

            case "USER":
            {
                string u = tokenizer.GetStringToken();

                tokenizer.GetThis("PASSWORD");

                string p = tokenizer.GetStringToken();
                bool   admin;

                if (tokenizer.GetString().Equals("ADMIN"))
                {
                    admin = true;
                }
                else
                {
                    admin = false;
                }

                _access.CreateUser(u, p, admin);
            }
            break;

            case "ALIAS":
            {
                string name = tokenizer.GetString();

                sToken = tokenizer.GetString();

                Trace.Check(sToken.Equals("FOR"), Trace.UnexpectedToken, sToken);

                sToken = tokenizer.GetString();

                _alias[name] = sToken;
            }
            break;

            default:
            {
                bool unique = false;

                if (sToken.Equals("UNIQUE"))
                {
                    unique = true;
                    sToken = tokenizer.GetString();
                }

                if (!sToken.Equals("INDEX"))
                {
                    throw Trace.Error(Trace.UnexpectedToken, sToken);
                }

                string name = tokenizer.GetName();

                tokenizer.GetThis("ON");

                Table t = GetTable(tokenizer.GetString(), channel);

                AddIndexOn(tokenizer, channel, name, t, unique);
            }
            break;
            }

            return(new Result());
        }
Example #4
0
        private Result ProcessCreate(Tokenizer tokenizer, Channel channel)
        {
            channel.CheckReadWrite();
            channel.CheckAdmin();

            string sToken = tokenizer.GetString();

            switch(sToken)
            {
                case "TABLE":
                    ProcessCreateTable(tokenizer, channel, true);
                    break;
                case "MEMORY":
                    tokenizer.GetThis("TABLE");
                    ProcessCreateTable(tokenizer, channel, false);
                    break;
                case "CACHED":
                    tokenizer.GetThis("TABLE");
                    ProcessCreateTable(tokenizer, channel, true);
                    break;
                case "USER":
                {
                    string u = tokenizer.GetStringToken();

                    tokenizer.GetThis("PASSWORD");

                    string  p = tokenizer.GetStringToken();
                    bool admin;

                    if (tokenizer.GetString().Equals("ADMIN"))
                    {
                        admin = true;
                    }
                    else
                    {
                        admin = false;
                    }

                    _access.CreateUser(u, p, admin);
                }
                    break;
                case "ALIAS":
                {
                    string name = tokenizer.GetString();

                    sToken = tokenizer.GetString();

                    TracingHelper.Check(sToken.Equals("FOR"), TracingHelper.UnexpectedToken, sToken);

                    sToken = tokenizer.GetString();

                    _alias[name] = sToken;
                }
                    break;
                default:
                {
                    bool unique = false;

                    if (sToken.Equals("UNIQUE"))
                    {
                        unique = true;
                        sToken = tokenizer.GetString();
                    }

                    if (!sToken.Equals("INDEX"))
                    {
                        throw TracingHelper.Error(TracingHelper.UnexpectedToken, sToken);
                    }

                    string name = tokenizer.GetName();

                    tokenizer.GetThis("ON");

                    Table t = GetTable(tokenizer.GetString(), channel);

                    AddIndexOn(tokenizer, channel, name, t, unique);
                }
                    break;
            }

            return new Result();
        }