@@ -449,19 +449,17 @@ protected virtual IEnumerable<MigrationStatement> Generate(HistoryOperation oper
449449 switch ( commandTree . CommandTreeKind )
450450 {
451451 case DbCommandTreeKind . Insert :
452- const int migrationIdColumn = 0 ;
453- const int contextKeyColumn = 1 ;
454- const int modelColumn = 2 ;
455- const int versionColumn = 3 ;
456-
457- // Trial and error value, not sure if correct or how to get correct one
458- const int maxChunkLength = 32000 ;
452+ const int MigrationIdColumn = 0 ;
453+ const int ContextKeyColumn = 1 ;
454+ const int ModelColumn = 2 ;
455+ const int VersionColumn = 3 ;
456+ const int MaxChunkLength = 32000 ;
459457
460458 var dbInsert = ( DbInsertCommandTree ) commandTree ;
461- var modelData = ( ( dbInsert . SetClauses [ modelColumn ] as DbSetClause ) . Value as DbConstantExpression ) . Value as byte [ ] ;
459+ var modelData = ( ( dbInsert . SetClauses [ ModelColumn ] as DbSetClause ) . Value as DbConstantExpression ) . Value as byte [ ] ;
462460
463461 // If model length is less than max value, stick to original version
464- if ( modelData . Length < maxChunkLength )
462+ if ( modelData . Length < MaxChunkLength )
465463 {
466464 using ( var writer = SqlWriter ( ) )
467465 {
@@ -472,30 +470,25 @@ protected virtual IEnumerable<MigrationStatement> Generate(HistoryOperation oper
472470 else
473471 {
474472 // If it's bigger - we split it into chunks, as big as possible
475- var dataChunks = modelData . Split ( maxChunkLength ) ;
473+ var dataChunks = modelData . Split ( MaxChunkLength ) ;
476474
477475 // We can't change CommandTree, but we can create new one, only difference being data length
478476 using ( var writer = SqlWriter ( ) )
479477 {
480- ReadOnlyCollection < DbModificationClause > setClauses = new ReadOnlyCollection < DbModificationClause > (
478+ var setClauses = new ReadOnlyCollection < DbModificationClause > (
481479 new List < DbModificationClause >
482480 {
483- dbInsert . SetClauses [ migrationIdColumn ] ,
484- dbInsert . SetClauses [ contextKeyColumn ] ,
481+ dbInsert . SetClauses [ MigrationIdColumn ] ,
482+ dbInsert . SetClauses [ ContextKeyColumn ] ,
485483 DbExpressionBuilder . SetClause (
486- ( ( DbSetClause ) dbInsert . SetClauses [ modelColumn ] ) . Property ,
484+ ( ( DbSetClause ) dbInsert . SetClauses [ ModelColumn ] ) . Property ,
487485 dataChunks . ElementAt ( 0 ) . ToArray ( )
488486 ) ,
489- dbInsert . SetClauses [ versionColumn ] ,
487+ dbInsert . SetClauses [ VersionColumn ] ,
490488 } ) ;
491489
492490
493- var newCommandTree = new DbInsertCommandTree (
494- dbInsert . MetadataWorkspace ,
495- commandTree . DataSpace ,
496- dbInsert . Target ,
497- setClauses ,
498- dbInsert . Returning ) ;
491+ var newCommandTree = new DbInsertCommandTree ( dbInsert . MetadataWorkspace , commandTree . DataSpace , dbInsert . Target , setClauses , dbInsert . Returning ) ;
499492
500493 writer . Write ( DmlSqlGenerator . GenerateInsertSql ( newCommandTree , out _ , generateParameters : false ) ) ;
501494 yield return Statement ( writer ) ;
@@ -506,10 +499,9 @@ protected virtual IEnumerable<MigrationStatement> Generate(HistoryOperation oper
506499 {
507500 using ( var writer = SqlWriter ( ) )
508501 {
509- DbPropertyExpression modelProperty = ( dbInsert . SetClauses [ modelColumn ] as DbSetClause ) . Property as DbPropertyExpression ;
502+ var modelProperty = ( dbInsert . SetClauses [ ModelColumn ] as DbSetClause ) . Property as DbPropertyExpression ;
510503
511- ReadOnlyCollection < DbModificationClause > modificationClauses = new ReadOnlyCollection < DbModificationClause > (
512- new List < DbModificationClause >
504+ var modificationClauses = new List < DbModificationClause >
513505 {
514506 // Updating existing chunk of data with subsequent part
515507 DbExpressionBuilder . SetClause (
@@ -519,15 +511,15 @@ protected virtual IEnumerable<MigrationStatement> Generate(HistoryOperation oper
519511 // Here we'll get SET Model = 'data', which we can update as text later
520512 dataChunk . ToArray ( )
521513 )
522- } ) ;
514+ } . AsReadOnly ( ) ;
523515
524516 var updateCommandTree = new DbUpdateCommandTree ( dbInsert . MetadataWorkspace ,
525517 dbInsert . DataSpace ,
526518 dbInsert . Target ,
527519 // Predicate is MigrationId value
528520 DbExpressionBuilder . Equal (
529- ( ( DbSetClause ) dbInsert . SetClauses [ migrationIdColumn ] ) . Property ,
530- ( ( DbSetClause ) dbInsert . SetClauses [ migrationIdColumn ] ) . Value ) ,
521+ ( ( DbSetClause ) dbInsert . SetClauses [ MigrationIdColumn ] ) . Property ,
522+ ( ( DbSetClause ) dbInsert . SetClauses [ MigrationIdColumn ] ) . Value ) ,
531523 modificationClauses ,
532524 dbInsert . Returning ) ;
533525
@@ -538,8 +530,7 @@ protected virtual IEnumerable<MigrationStatement> Generate(HistoryOperation oper
538530 // with SET Model = Model || 'data'
539531 // Model being first is important, since these are parts of single value
540532 var statement = writer . ToString ( ) ;
541- var newStatement = statement . Replace ( $ "SET \" { modelProperty . Property . Name } \" = ",
542- $ "SET \" { modelProperty . Property . Name } \" = \" { modelProperty . Property . Name } \" || ") ;
533+ var newStatement = statement . Replace ( $ "SET \" { modelProperty . Property . Name } \" = ", $ "SET \" { modelProperty . Property . Name } \" = \" { modelProperty . Property . Name } \" || ") ;
543534
544535 yield return Statement ( newStatement ) ;
545536 }
@@ -549,8 +540,7 @@ protected virtual IEnumerable<MigrationStatement> Generate(HistoryOperation oper
549540 case DbCommandTreeKind . Delete :
550541 using ( var writer = SqlWriter ( ) )
551542 {
552- writer . Write ( DmlSqlGenerator . GenerateDeleteSql ( ( DbDeleteCommandTree ) commandTree , out _ ,
553- generateParameters : false ) ) ;
543+ writer . Write ( DmlSqlGenerator . GenerateDeleteSql ( ( DbDeleteCommandTree ) commandTree , out _ , generateParameters : false ) ) ;
554544 yield return Statement ( writer ) ;
555545 }
556546 break ;
0 commit comments