Exemple #1
0
        //we need to do the work around. http://www.sqlite.org/faq.html#q11
        public IEnumerable <string> CreateSqlStatements(AlterTableCommand command)
        {
            string fullTableName = command.Name;
            string tempTableName = fullTableName + "_" + Guid.NewGuid().ToString("N");


            yield return(StatementUtil.CreateTempTable(tempTableName, fullTableName));


            //modify all the references to the old table to go to the new table

            //drop the old table
            yield return(StatementUtil.DropTable(fullTableName));

            //create newTable and insert the data, add and drop indices
            foreach (string statement in CreateNewTableCopyInDataAndCreateIndices(command, tempTableName))
            {
                yield return(statement);
            }

            //change them all back!

            //drop the tempTable
            yield return(StatementUtil.DropTable(tempTableName));
        }
Exemple #2
0
        private IEnumerable <string> CreateNewTableCopyInDataAndCreateIndices(AlterTableCommand command, string tempTable)
        {
            string fullTableName = command.Name;

            //we'll use this to create the insert into later
            string[] originalColumns = CreateTableNode.ColumnDefinitions.Select(i => i.ColumnName).ToArray();

            CreateTableNode = IncorporateAlterationsInCreateNode(command, CreateTableNode);

            var visitor = new TreeStringOutputVisitor();

            yield return(CreateTableNode.Accept(visitor).ToString());

            string[] finalSetOfColumns =
                originalColumns.Where(i => command.TableCommands.OfType <DropColumnCommand>().All(j => j.ColumnName != i))
                .ToArray();

            // this can only be the ones in the ORIGINAL table that we want to copy, i.e. don't copy in deleted columns
            yield return(StatementUtil.InsertInto(fullTableName, finalSetOfColumns, tempTable));

            //let's figure out our final indexes
            IncorporateAlterationsInIndexNodes(CreateIndexNodes, fullTableName, command.TableCommands, finalSetOfColumns);
            var indexNodeVisitor = new TreeStringOutputVisitor();

            foreach (CreateIndexNode indexNode in CreateIndexNodes)
            {
                yield return(indexNode.Accept(indexNodeVisitor).ToString());
            }
        }
        /// <summary>
        /// returns null if something if we don't have ones we can do natively
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public IEnumerable <string> CreateSqlStatements(AlterTableCommand command)
        {
            if (!command.TableCommands.All(i => i is AddColumnCommand || i is AddIndexCommand || i is DropIndexCommand))
            {
                // we have non Native Alterations to make
                yield break;
            }
            var visitor = new TreeStringOutputVisitor();

            foreach (var alterCommand in command.TableCommands)
            {
                if (alterCommand is AddColumnCommand)
                {
                    var addColumn = alterCommand as AddColumnCommand;
                    yield return
                        (StatementUtil.AddColumn(command.Name,
                                                 addColumn.CreateColumnDefNode().Accept(visitor).ToString()));
                }
                else if (alterCommand is AddIndexCommand)
                {
                    var addIndexCommand = alterCommand as AddIndexCommand;

                    yield return(StatementUtil.AddIndex(addIndexCommand.IndexName, addIndexCommand.TableName, addIndexCommand.ColumnNames));
                }
                else if (alterCommand is DropIndexCommand)
                {
                    var dropIndexCommand = alterCommand as DropIndexCommand;
                    yield return(StatementUtil.DropIndex(dropIndexCommand.IndexName));
                }
            }
        }