Example #1
0
        public void Should_find_proper_bucket_for_int32()
        {
            var s = new KNodeId32(0);

            KTable.GetBucketIndex(s, new KNodeId32(1)).Should().Be(0);
            KTable.GetBucketIndex(s, new KNodeId32(2)).Should().Be(1);
            KTable.GetBucketIndex(s, new KNodeId32(2147483648)).Should().Be(31);
        }
        public CSeedScript Convert(KTable kTable)
        {
            var seedScript = new CSeedScript();
            var codeWriter = new CodeWriter();

            if (kTable.GeneratedTable.Row.Count == 0)
            {
                codeWriter.WriteLine($"-- Sample data has not been supplied for {kTable.GeneratedTable.TableName} ");
            }
            var firstRow = true;

            foreach (var row in kTable.GeneratedTable.Row)
            {
                if (!firstRow)
                {
                    codeWriter.WriteLine("UNION ALL");
                }
                codeWriter.Write("INSERT INTO (");
                var firstColumnHeader = true;
                foreach (var column in kTable.GeneratedTable.Column)
                {
                    if (!firstColumnHeader)
                    {
                        codeWriter.Write(", ");
                    }
                    codeWriter.Write($"[{column.ColumnName}]");

                    firstColumnHeader = false;
                }
                codeWriter.Write(") VALUES (");

                var firstColumnValue = true;
                foreach (var column in kTable.GeneratedTable.Column)
                {
                    if (!firstColumnValue)
                    {
                        codeWriter.Write(", ");
                    }

                    var data = row.RowData.FirstOrDefault(rd => rd.Column.ColumnName == column.ColumnName);
                    if (data != null)
                    {
                        codeWriter.Write($"{data}");
                    }
                    else
                    {
                        codeWriter.Write("NULL");
                    }
                    firstColumnValue = false;
                }
                codeWriter.WriteLine(")");

                firstRow = false;
            }

            seedScript.SeedScriptBody = codeWriter.ToString();
            return(seedScript);
        }
        protected CClass GetTableDto(IEnumerable <KTable> allTables, KTable table, string tableName)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                return(null);
            }

            var converter = new CTableToCClassConverter();
            var @class    = converter.Convert(table.GeneratedTable, allTables.Select(kt => kt.GeneratedTable), false);

            @class.ClassName  += "Dto";
            @class.DerivedFrom = table;
            //overrite the default namespace logic
            @class.Namespace = new CNamespace
            {
                NamespaceName =
                    $"{_dataLayerKProject.CompanyName}.{_dataLayerKProject.ProjectName}{_dataLayerKProject.NamespaceSuffix}.Data.Entities"
            };
            return(@class);
        }
        private List <KTable> CreateSqlTablesInDb(KDataStoreProject kDataStoreProject, string connectionString, string sqlText)
        {
            var tablesNames = new List <KTable>();
            var scriptGen   = new Sql120ScriptGenerator();

            using (var sqlConnection = new SqlConnection(connectionString))
            {
                sqlConnection.Open();

                var parser = new TSql120Parser(false);


                var script2 = parser.Parse(new StringReader(sqlText), out var errors) as TSqlScript;
                if (errors.Count > 0)
                {
                    var errorList = new StringBuilder();
                    foreach (var error in errors)
                    {
                        errorList.AppendLine($"{error.Message}<br/>");
                    }
                    throw new ApplicationException(errorList.ToString());
                }
                foreach (var batch2 in script2.Batches)
                {
                    foreach (var statement in batch2.Statements)
                    {
                        var createTableStatement = statement as CreateTableStatement;
                        var alterTableStatement  = statement as AlterTableStatement;

                        if (createTableStatement == null && alterTableStatement == null)
                        {
                            continue;
                        }

                        string viewSchemaName = string.Empty;

                        if (createTableStatement != null)
                        {
                            viewSchemaName = createTableStatement.SchemaObjectName.SchemaIdentifier.Value;
                        }
                        else if (alterTableStatement != null)
                        {
                            viewSchemaName = alterTableStatement.SchemaObjectName.SchemaIdentifier.Value;
                        }

                        var createSchemaCommand = new SqlCommand(
                            $@" IF NOT EXISTS (SELECT name FROM sys.schemas WHERE name = N'{viewSchemaName}')
                                        BEGIN
                                            EXEC sp_executesql N'CREATE SCHEMA [{viewSchemaName}]'
                                        END", sqlConnection);
                        createSchemaCommand.ExecuteNonQuery();

                        scriptGen.GenerateScript(statement, out var scriptOut);

                        var sqlCommand = new SqlCommand(scriptOut, sqlConnection);
                        sqlCommand.ExecuteNonQuery();

                        if (createTableStatement != null)
                        {
                            var oldKTable = kDataStoreProject.OldTable.FirstOrDefault(t =>
                                                                                      t.Schema.SchemaName == createTableStatement.SchemaObjectName.SchemaIdentifier.Value &&
                                                                                      t.TableName == createTableStatement.SchemaObjectName.BaseIdentifier.Value);

                            //copy some attributes

                            var newKTable = new KTable
                            {
                                Schema =
                                    new CSchema
                                {
                                    SchemaName = createTableStatement.SchemaObjectName.SchemaIdentifier.Value
                                },
                                TableName =
                                    createTableStatement.SchemaObjectName.BaseIdentifier.Value,
                                TableText = scriptOut
                            };

                            if (oldKTable != null)
                            {
                                newKTable.DerivedFrom = oldKTable.DerivedFrom;
                            }

                            tablesNames.Add(newKTable);
                        }
                        else if (alterTableStatement != null)
                        {
                            //assumes ALTER TABLE come before CREATE TABLE
                            var existingTable = tablesNames.Single(t =>
                                                                   t.Schema.SchemaName == alterTableStatement.SchemaObjectName.SchemaIdentifier.Value &&
                                                                   t.TableName == alterTableStatement.SchemaObjectName.BaseIdentifier.Value);

                            existingTable.TableText += Environment.NewLine + "GO" + Environment.NewLine + Environment.NewLine;
                            existingTable.TableText += scriptOut;
                            existingTable.TableText += Environment.NewLine + "GO" + Environment.NewLine + Environment.NewLine;
                        }
                    }
                }

                sqlConnection.Close();
            }
            return(tablesNames);
        }