예제 #1
0
        public static CreateDatabaseStatement CreateDatabase(Identifier databaseName)
        {
            CreateDatabaseStatement fragment = new CreateDatabaseStatement();

            fragment.DatabaseName = databaseName;
            return(fragment);
        }
            public override void Visit(CreateDatabaseStatement node)
            {
                foreach (FileGroupDefinition fg in node.FileGroups)
                {
                    foreach (FileDeclaration fileDecl in fg.FileDeclarations)
                    {
                        // The Primary filegroup is where the database's MDF file will be present

                        if (fileDecl.IsPrimary)
                        {
                            NameFileDeclarationOption nameOption = fileDecl.Options.SingleOrDefault(
                                opt => opt.OptionKind == FileDeclarationOptionKind.Name) as NameFileDeclarationOption;
                            if (nameOption == null)
                            {
                                continue;
                            }

                            // Verify that the filename has the pattern "$(DatabaseName)", and overwrite
                            if (string.Compare(nameOption.LogicalFileName.Identifier.Value,
                                               DatabaseNameVariable,
                                               StringComparison.OrdinalIgnoreCase) == 0)
                            {
                                FileNameFileDeclarationOption fileNameOption = fileDecl.Options.SingleOrDefault(
                                    opt => opt.OptionKind == FileDeclarationOptionKind.FileName) as FileNameFileDeclarationOption;
                                if (fileNameOption != null)
                                {
                                    fileNameOption.OSFileName = new StringLiteral {
                                        Value = _databaseFileName, IsNational = false
                                    };
                                }
                            }
                        }
                    }
                }

                foreach (FileDeclaration logFile in node.LogOn)
                {
                    NameFileDeclarationOption nameOption = logFile.Options.SingleOrDefault(
                        opt => opt.OptionKind == FileDeclarationOptionKind.Name) as NameFileDeclarationOption;
                    if (nameOption == null)
                    {
                        continue;
                    }

                    if (string.Compare(nameOption.LogicalFileName.Identifier.Value,
                                       DatabaseLogNameVariable,
                                       StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        FileNameFileDeclarationOption fileNameOption = logFile.Options.SingleOrDefault(
                            opt => opt.OptionKind == FileDeclarationOptionKind.FileName) as FileNameFileDeclarationOption;
                        if (fileNameOption != null)
                        {
                            fileNameOption.OSFileName = new StringLiteral {
                                Value = _logFileName, IsNational = false
                            };
                        }
                    }
                }
            }
        public void CreateStatementWithOneKeyTest()
        {
            var statements = new List<IStatement>
            {
                CreateStatementMock("dummy1").Object
            };

            var createDatabaseStatement = new CreateDatabaseStatement(statements);
            Assert.AreEqual(createDatabaseStatement.Count, 1);
            Assert.AreEqual(createDatabaseStatement.CreateStatement(), "dummy1");
        }
예제 #4
0
    // end create table functions

    // begin create database functions
    public override void EnterCreate_database([NotNull] TSqlParser.Create_databaseContext context)
    {
        base.EnterCreate_database(context);
        Debug.WriteLine(context.GetText());

        if (IsStatementCreateDatabase())
        {
            CreateDatabaseStatement statement = GetStatementAsCreateDatabase();
            statement.RawTextWithWhitespace = GetWhiteSpaceFormat(context);
            statement.DatabaseName          = statement.RawTextWithWhitespace.Replace(QueryKeywords.CREATE_DATABASE, string.Empty).Trim();
        }
    }
        public void CreateStatementWithOneKeyTest()
        {
            var statements = new List <IStatement>
            {
                CreateStatementMock("dummy1").Object
            };

            var createDatabaseStatement = new CreateDatabaseStatement(statements);

            Assert.AreEqual(createDatabaseStatement.Count, 1);
            Assert.AreEqual(createDatabaseStatement.CreateStatement(), "dummy1");
        }
 public override void ExplicitVisit(CreateDatabaseStatement node)
 {
     Visit(node);
 }
예제 #7
0
 public override void Visit(CreateDatabaseStatement node) { this.action(node); }
 public override void ExplicitVisit(CreateDatabaseStatement fragment)
 {
     _fragments.Add(fragment);
 }
예제 #9
0
파일: Parser.cs 프로젝트: fizikci/Cinar
        CreateDatabaseStatement ParseCreateDatabaseStatement()
        {
            CreateDatabaseStatement ss = new CreateDatabaseStatement();

            ReadNextToken(); //skip "database"

            ss.DatabaseName = fCurrentToken.Value.TrimQuotation();

            ReadNextToken();

            return ss;
        }
예제 #10
0
        public KDataStoreProject BuildSqlMeta(string connectionString, string outputRootPath,
                                              KDataStoreProject dataStoreProject)
        {
            dataStoreProject.ClearSqlMeta(); //clear what was generated during user config, going to regenerate
            var stopWatch = Stopwatch.StartNew();

            var connectionStringBuilder = new SqlConnectionStringBuilder(connectionString);
            var timeString = DateTime.Now.ToString("yyyyMMddHHmmss");
            var newDbName  = $"WorkDb{timeString}";

            connectionStringBuilder.InitialCatalog     = "master";
            connectionStringBuilder.IntegratedSecurity = true;
            //connectionStringBuilder.UserID = "fillthisin";
            //connectionStringBuilder.Password = "******";
            connectionStringBuilder.ConnectTimeout = 60;

            var scriptGen = new Sql120ScriptGenerator();


            using (var sqlConnection = new SqlConnection(connectionStringBuilder.ConnectionString))
            {
                sqlConnection.Open();
                var createDatabaseStatement =
                    new CreateDatabaseStatement {
                    DatabaseName = new Identifier {
                        Value = newDbName
                    }
                };

                string createDatabaseScriptOut;
                scriptGen.GenerateScript(createDatabaseStatement, out createDatabaseScriptOut);
                var sqlCommandCreateDatabase = new SqlCommand(createDatabaseScriptOut, sqlConnection);
                sqlCommandCreateDatabase.ExecuteNonQuery();
                sqlConnection.Close();
            }

            connectionStringBuilder.InitialCatalog = newDbName;

            if (!string.IsNullOrEmpty(dataStoreProject.SqlTableText))
            {
                var tableNames = CreateSqlTablesInDb(dataStoreProject, connectionStringBuilder.ConnectionString,
                                                     dataStoreProject.SqlTableText);

                foreach (var tablePair in tableNames)
                {
                    _sqlServerTableReader.ConnectionString = connectionStringBuilder.ConnectionString;
                    var dataReader     = _sqlServerTableReader.Read(tablePair.Schema.SchemaName, tablePair.TableName);
                    var foreignKeyData = _sqlServerTableReader.ReadForeignKeys(tablePair.Schema.SchemaName, tablePair.TableName);
                    var table2         = _sqlServerTableToCTableConverter.Convert(dataReader, foreignKeyData);
                    table2.TableText          = tablePair.TableText;
                    table2.DatabaseType       = dataStoreProject.DataStoreType;
                    table2.ConvertToSnakeCase = dataStoreProject.ConvertToSnakeCase;

                    tablePair.GeneratedTable = table2;
                    dataStoreProject.Table.Add(tablePair);
                }

                _sqlServerTableToCTableConverter.FixupForeignKeyTables(dataStoreProject.Table.Select(kt => kt.GeneratedTable));
                foreach (var tablePair in tableNames)
                {
                    tablePair.GeneratedTable.DerivedFrom = tablePair.DerivedFrom;
                }
            }

            if (!string.IsNullOrEmpty(dataStoreProject.MockSqlViewText))
            {
                var mockViewNames = CreateMockSqlViewsInDb(connectionStringBuilder.ConnectionString,
                                                           dataStoreProject.MockSqlViewText);


                foreach (var viewPair in mockViewNames)
                {
                    var dataReader = _sqlServerViewReader.Read(connectionStringBuilder.ConnectionString, viewPair.ViewName, viewPair.ViewText);
                    var view2      = _sqlServerViewToCViewConverter.Convert(dataReader);

                    if (dataStoreProject.KickstartCreatedBy)
                    {
                        AddCreatedByToView(view2);
                    }
                    if (dataStoreProject.KickstartCreatedDateUtc)
                    {
                        AddCreatedDateUtcToView(view2);
                    }
                    if (dataStoreProject.KickstartModifiedBy)
                    {
                        AddModifiedByToView(view2);
                    }
                    if (dataStoreProject.KickstartModifiedDateUtc)
                    {
                        AddModifiedDateUtcToView(view2);
                    }
                    if (dataStoreProject.KickstartLoadDateUtc)
                    {
                        AddLoadDateUtcToView(view2);
                    }
                    if (dataStoreProject.KickstartEffectiveDateUtc)
                    {
                        AddEffectiveDateUtcToView(view2);
                    }
                    if (dataStoreProject.KickstartHashDiff)
                    {
                        AddHashDiffToView(view2);
                    }

                    viewPair.GeneratedView = view2;
                    dataStoreProject.MockView.Add(viewPair);
                }
            }
            if (!string.IsNullOrEmpty(dataStoreProject.SqlTableTypeText))
            {
                var tableTypeNames = CreateTableTypesInDb(dataStoreProject, connectionStringBuilder.ConnectionString,
                                                          dataStoreProject.SqlTableTypeText);


                foreach (var tableType in tableTypeNames)
                {
                    _sqlServerTableTypeReader.ConnectionString = connectionStringBuilder.ConnectionString;
                    var tt = _sqlServerTableTypeReader.Read(tableType.Schema, tableType.TableTypeName, tableType.TableTypeText);
                    tt.DerivedFrom        = tableType.DerivedFrom as CPart;
                    tt.DatabaseType       = dataStoreProject.DataStoreType;
                    tt.ConvertToSnakeCase = dataStoreProject.ConvertToSnakeCase;

                    tableType.GeneratedTableType = tt;

                    dataStoreProject.TableType.Add(tableType);
                }
            }
            if (!string.IsNullOrEmpty(dataStoreProject.SqlViewText))
            {
                var viewNames =
                    CreateSqlViewsInDb(connectionStringBuilder.ConnectionString, dataStoreProject.SqlViewText);

                foreach (var viewPair in viewNames)
                {
                    var dataReader = _sqlServerViewReader.Read(connectionStringBuilder.ConnectionString, viewPair.ViewName, viewPair.ViewText);

                    var view2 = _sqlServerViewToCViewConverter.Convert(dataReader);
                    view2.DatabaseType       = dataStoreProject.DataStoreType;
                    view2.ConvertToSnakeCase = dataStoreProject.ConvertToSnakeCase;

                    view2.ViewText         = viewPair.ViewText;
                    viewPair.GeneratedView = view2;
                    dataStoreProject.View.Add(viewPair);
                }
            }
            if (!string.IsNullOrEmpty(dataStoreProject.SqlFunctionText))
            {
                var functionNames =
                    CreateSqlFunctionsInDb(dataStoreProject, connectionStringBuilder.ConnectionString, dataStoreProject.SqlFunctionText);

                foreach (var functionPair in functionNames)
                {
                    var function2 = _sqlServerFunctionReader.Read(connectionStringBuilder.ConnectionString, functionPair.Schema.SchemaName, functionPair.FunctionName, functionPair.FunctionText);

                    //var function2 = _sqlServerFunctionToCFunctionConverter.Convert(dataReader);
                    functionPair.DatabaseType       = dataStoreProject.DataStoreType;
                    functionPair.ConvertToSnakeCase = dataStoreProject.ConvertToSnakeCase;

                    functionPair.FunctionText      = functionPair.FunctionText;
                    functionPair.GeneratedFunction = functionPair;
                    dataStoreProject.Function.Add(functionPair);
                }
            }

            if (!string.IsNullOrEmpty(dataStoreProject.SqlStoredProcedureText))
            {
                var storedProcedureNames = CreateSqlStoredProcsInDb(dataStoreProject, connectionStringBuilder.ConnectionString,
                                                                    dataStoreProject.SqlStoredProcedureText);


                foreach (var sp in storedProcedureNames)
                {
                    var storedProceure = _sqlServerStoredProcedureReader.Read(connectionStringBuilder.ConnectionString, sp.Schema, sp.StoredProcedureName, sp.StoredProcedureText);
                    storedProceure.DatabaseType       = dataStoreProject.DataStoreType;
                    storedProceure.DerivedFrom        = sp.DerivedFrom as CPart;
                    storedProceure.ConvertToSnakeCase = dataStoreProject.ConvertToSnakeCase;

                    sp.GeneratedStoredProcedure = storedProceure;
                    sp.GeneratedStoredProcedure.StoredProcedureDescription = sp.StoredProcedureDescription;
                    sp.GeneratedStoredProcedure.GenerateAsEmbeddedQuery    = dataStoreProject.GenerateStoredProcAsEmbeddedQuery;
                    dataStoreProject.StoredProcedure.Add(sp);
                }
            }

            if (dataStoreProject.Query.Any())
            {
                foreach (var kQuery in dataStoreProject.Query)
                {
                    var reader =
                        new SqlServerQueryReader
                    {
                        ConnectionString = connectionStringBuilder.ConnectionString
                    };

                    kQuery.GeneratedQuery = new CQuery();
                    //databaseProject.Query.Add()

                    /*
                     * var query = reader.Read(sp.Schema, sp.StoredProcedureName, sp.StoredProcedureText);
                     * sp.GeneratedStoredProcedure = storedProceure;
                     * sp.GeneratedStoredProcedure.StoredProcedureDescription = sp.StoredProcedureDescription;
                     * databaseProject.StoredProcedure.Add(sp);
                     */
                }
            }

            DropWorkDb(connectionStringBuilder, newDbName);


            dataStoreProject.ConfigureMetaData();


            dataStoreProject.AddSeedData();
            ConvertMockViewsToTables(dataStoreProject);
            var currentSPs = new List <KStoredProcedure>();

            currentSPs.AddRange(dataStoreProject.StoredProcedure);
            dataStoreProject.StoredProcedure.Clear();
            if (dataStoreProject.KickstartCRUDStoredProcedures)
            {
                AddInsertUpdateStoredProcedures(dataStoreProject);
            }
            dataStoreProject.StoredProcedure.AddRange(currentSPs);
            if (dataStoreProject.KickstartCRUDStoredProcedures)
            {
                AddDeleteStoredProcedures(dataStoreProject);
            }
            dataStoreProject.ConfigureMetaData2();

            //todo; clean this up
            foreach (var sp in dataStoreProject.StoredProcedure)
            {
                sp.GeneratedStoredProcedure.ParameterSetName    = sp.ParameterSetName;
                sp.GeneratedStoredProcedure.ResultSetName       = sp.ResultSetName;
                sp.GeneratedStoredProcedure.ReturnsMultipleRows = sp.ReturnsMultipleRows;
            }

            /*
             * var protoRpcRefs = new List<SProtoRpcRef>();
             * foreach (var protoRpcRef in mSolution.ProtoRpcRef)
             * {
             *
             *  protoRpcRefs.Add(protoRpcRef);
             * }*/
            return(dataStoreProject);
        }
            public override void Visit(CreateDatabaseStatement node)
            {
                foreach (FileGroupDefinition fg in node.FileGroups)
                {
                    foreach (FileDeclaration fileDecl in fg.FileDeclarations)
                    {
                        // The Primary filegroup is where the database's MDF file will be present

                        if (fileDecl.IsPrimary)
                        {
                            NameFileDeclarationOption nameOption = fileDecl.Options.SingleOrDefault(
                                opt => opt.OptionKind == FileDeclarationOptionKind.Name) as NameFileDeclarationOption;
                            if (nameOption == null)
                            {
                                continue;
                            }

                            // Verify that the filename has the pattern "$(DatabaseName)", and overwrite
                            if (string.Compare(nameOption.LogicalFileName.Identifier.Value,
                                                DatabaseNameVariable,
                                                StringComparison.OrdinalIgnoreCase) == 0)
                            {
                                FileNameFileDeclarationOption fileNameOption = fileDecl.Options.SingleOrDefault(
                                    opt => opt.OptionKind == FileDeclarationOptionKind.FileName) as FileNameFileDeclarationOption;
                                if (fileNameOption != null)
                                {
                                    fileNameOption.OSFileName = new StringLiteral { Value = _databaseFileName, IsNational = false };
                                }
                            }

                        }
                    }
                }

                foreach (FileDeclaration logFile in node.LogOn)
                {
                    NameFileDeclarationOption nameOption = logFile.Options.SingleOrDefault(
                               opt => opt.OptionKind == FileDeclarationOptionKind.Name) as NameFileDeclarationOption;
                    if (nameOption == null)
                    {
                        continue;
                    }

                    if (string.Compare(nameOption.LogicalFileName.Identifier.Value,
                                                DatabaseLogNameVariable,
                                                StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        FileNameFileDeclarationOption fileNameOption = logFile.Options.SingleOrDefault(
                            opt => opt.OptionKind == FileDeclarationOptionKind.FileName) as FileNameFileDeclarationOption;
                        if (fileNameOption != null)
                        {
                            fileNameOption.OSFileName = new StringLiteral { Value = _logFileName, IsNational = false };
                        }
                    }
                }
            }
 public override void ExplicitVisit(CreateDatabaseStatement node)
 {
     Visit(node);
 }