-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySqlToolsFixes.vb
676 lines (589 loc) · 36.2 KB
/
MySqlToolsFixes.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
Imports MySql.Data.MySqlClient
Imports EaTools.Ea
Imports EaTools.DataTools
Partial Public Class MySqlTools
#Region " Class Constants, ENums and Variables "
#Region " Constants "
Public Const NonLinkedAdded As String = "NL"
Public Const NonLinkedTableName As String = "NonLinked"
#End Region
#Region " Variables "
Private Shared _fixedMessage As String = String.Empty
Private Shared FixingNonLinkedRows As Boolean = False
#End Region
#End Region
#Region " Properties "
Public Shared ReadOnly Property FixedMessage As String
Get
Return _fixedMessage
End Get
End Property
#End Region
#Region " Fix Concurrency Exception "
Public Shared Function FixConcurrencyException(MySqlConn As MySqlConnection,
rowWithConcurEx As DataRow,
rurRowInDb As DataRow,
tempTable As DataTable) As FixedConcurExTypes
' tries to fix Concurrency error based on value in UpdatedWhen column
'
' vars passed:
' MySqlConn - mySql connection
' rowWithErr - row with error
' curRowInDb - current row in database
' tempTable - temp table used when fixing concurrency error
'
' returns:
' FixedConcurExTypes.NeedToUpdate - fixed, need to re-call update(table, false)
' FixedConcurExTypes.NeedToRefresh - fixed, need to refresh table
' FixedConcurExTypes.CouldNotFix - could not fixed
' - already fixing table
' - added wrong # of rows
' - error inserting row
' - error keeping row
' - something went wrong
Try
If Not tempTable.DataSet Is Nothing Then ' if temp table already in a dataset
_errorMessage = String.Format("Table ""{0}"" is currently fixing another Concurrency error.", tempTable.TableName)
_errorCode = teAlreadyFixing
Return MySqlTools.FixedConcurExTypes.CouldNotFix
End If
Using tempDS As New DataSet
tempDS.Tables.Add(tempTable) ' add temp table to temp data set
Try
Dim FixedType As FixedConcurExTypes
If rurRowInDb Is Nothing Then ' if other user deleted row
Dim rowCount As Integer
If rowWithConcurEx.RowState = DataRowState.Deleted Then ' if RowWithConcurEx was deleted
Return FixedConcurExTypes.NeedToRefresh
Else ' else other user delete row
Dim origRow As DataRow = GetDataRow(rowWithConcurEx, DataRowVersion.Original) ' get original row
rowCount = AddRowViaSql(MySqlConn, rowWithConcurEx.Table.TableName, origRow) ' add original row back to database
If rowCount = 0 OrElse rowCount > 1 Then ' if did not ad 1 row
_errorMessage = String.Format("Wrong number ({0}) of rows inserted", rowCount)
_errorCode = teWrongRowCountErr
Return FixedConcurExTypes.CouldNotFix
ElseIf rowCount < 0 Then ' else if got an error
_errorMessage = "Error inserting row"
_errorCode = rowCount
Return FixedConcurExTypes.CouldNotFix
End If
tempTable.ImportRow(origRow) ' import orig row into temp table
FixedType = KeepRow(rowWithConcurEx, rowWithConcurEx.Table, tempTable) ' keep row with concur ex
If FixedType = FixedConcurExTypes.CouldNotFix Then ' if error keeping row
' errorMessage and _error code set in KeepRow()
Return FixedConcurExTypes.CouldNotFix
End If
Return FixedConcurExTypes.NeedToUpdate
End If
Else ' else user deleted or edited row
Dim RowUpdatedWhen As Date = GcrcvAs(rowWithConcurEx, tsUpdatedWhenColName, NoDate) ' get when row with concur ex updated
Dim CrUpdatedWhen As Date = GcrcvAs(rurRowInDb, tsUpdatedWhenColName, NoDate) ' get when current row updated
If RowUpdatedWhen <> NoDate AndAlso CrUpdatedWhen <> NoDate Then ' if got an update for either row
If RowUpdatedWhen >= CrUpdatedWhen Then ' if concur ex row has most recent data
FixedType = KeepRow(rowWithConcurEx, rowWithConcurEx.Table, tempTable) ' keep row with concur ex
If FixedType = FixedConcurExTypes.CouldNotFix Then ' if error keeping row
' errorMessage and _error code set in KeepRow()
Return FixedConcurExTypes.CouldNotFix
End If
Return FixedConcurExTypes.NeedToUpdate
Else ' else current row has most recent data
FixedType = KeepRow(rurRowInDb, rowWithConcurEx.Table, tempTable) ' keep current db row
If FixedType = FixedConcurExTypes.CouldNotFix Then ' if error keeping row
' errorMessage and _error code set in KeepRow()
Return FixedConcurExTypes.CouldNotFix
End If
Return FixedConcurExTypes.NeedToUpdate
End If
Else ' else no updated when for either row
FixedType = KeepRow(rowWithConcurEx, rowWithConcurEx.Table, tempTable) ' keep row with concur ex
If FixedType = FixedConcurExTypes.CouldNotFix Then ' if error keeping row
' errorMessage and _error code set in KeepRow()
Return FixedConcurExTypes.CouldNotFix
End If
Return FixedConcurExTypes.NeedToUpdate
End If
End If
' if got here the fixed error
'Return 1 ' one row's concurrency error fixed
Catch ex As Exception
_errorMessage = String.Format("Fixing concurrency error for table ""{0}"". {1}", tempTable.TableName, ex.Message)
_errorCode = teConcurrencyErr
Return FixedConcurExTypes.CouldNotFix
End Try
End Using
Catch ex As Exception
_errorMessage = String.Format(efOther, ex.Message)
_errorCode = ex.HResult
Return FixedConcurExTypes.CouldNotFix
End Try
End Function
Public Shared Function GetCurrentRowInDb(MySqlConn As MySqlConnection, RowWithErr As DataRow, tempTable As DataTable) As DataRow
' tries to retrieve the current row from the database, based on the key field values in RowWithError
'
' note: TempTable will be left with either 1 (RowWithError found) or 0 (RowWithError NOT found) rows
'
' vars passed:
' MySqlConn - MySQL connection to user
' RowWithErr - row with error
' TempTable - temp table used when fixing concurrency error
'
' returns:
' DataRow - current matching row in the database
' Nothing -
' row's table not keyed
' row was not found in table in database
' more than 1 matching row was found in database
' some other error
Const SelectSQL As String = "SELECT * FROM "
Try
If RowWithErr.Table.PrimaryKey.Length = 0 Then ' if table is not keyed
Return Nothing ' return nothing
End If
' SELECT * FROM tablename
' WHERE (ColumnName1 = Value1)
Dim WhereSQL As String = GetWhereSqlForRow(RowWithErr) ' to select just row with error
Dim SelectSQLText As String = String.Format("{0} {1} {2}", SelectSQL, RowWithErr.Table.TableName, WhereSQL) ' get full select command text
' create table adapter with just select command
Dim tableAdapt As MySqlDataAdapter = CreateMySqlDataAdapt(MySqlConn, RowWithErr.Table.TableName, SelectSQLText, "", "", "")
Dim rowCount As Integer = MySqlTools.FillTable(tempTable, tableAdapt) ' fill just current values for rowWithErr
If rowCount = 0 OrElse rowCount > 1 Then ' if filled wrong # of rows
_errorMessage = String.Format("Wrong row count ({0})", rowCount)
_errorCode = teWrongRowCountErr
Return Nothing ' return nothing
ElseIf rowCount < 0 Then ' if got an error
' _errorMessage and _errorCode set in Fill
Return Nothing ' return error
End If
Return tempTable.Rows(0) ' current row is only row in temp table
'Return GetCurrentRowInDb(RowWithErr, tempTable) ' return just current row in database
Catch ex As Exception
_errorMessage = String.Format(efOther, ex.Message)
_errorCode = ex.HResult
Return Nothing
End Try
End Function
Private Shared Function KeepRow(rowToKeep As DataRow,
table As DataTable,
tempTable As DataTable) As FixedConcurExTypes
' enables keeping of a row when the row has been changed by another user
' for example
' 1) user x fills table z
' 2) user x makes changes to row 1
' 3) user y fills table z
' 4) user y makes changes to row 1
' 5) user y updates row 1 in table z
' 6) user x updates row 1 in table z (causes Concurrency Exception)
' this func will enable step 6 to complete
'
' vars passed:
' rowToKeep - row values to be kept (can be current user's row or current row in database)
' TempTable - table with copy of current row data in database
'
' returns
' 1 - 1 row updated
' 0 - update did not work
' teNoPrimaryKey - no primary key for rowToKeep
' teRowNotFound - matching row not found in current database table
' teOtherErr - something went wrong
Try
Dim KeyVals() As Object = GetRowKeyValues(rowToKeep) ' get key values
If KeyVals Is Nothing Then ' if no key values
_errorMessage = "No primary key"
_errorCode = teNoPrimaryKey
Return FixedConcurExTypes.CouldNotFix
End If
Dim t2Table As DataTable = rowToKeep.Table.Clone ' make a clone of the temp table
t2Table.Rows.Add(rowToKeep.ItemArray) ' add row to keep to temp table 2
MergeTableIntoDataSet(tempTable, table.DataSet, PreserveChangesTypes.Yes) ' merge current data
Dim rowToRestore As DataRow = FindRow(table, KeyVals) ' find row to restore
If rowToRestore Is Nothing Then ' if did not find row to restore
_errorMessage = "Could not find row to restore"
_errorCode = teRowNotFound
Return FixedConcurExTypes.CouldNotFix
End If
CopyRow(t2Table.Rows(0), rowToRestore) ' restore row values (user's edits)
Return FixedConcurExTypes.NeedToUpdate
Catch ex As Exception
_errorMessage = String.Format(efOther, ex.Message)
_errorCode = ex.HResult
Return FixedConcurExTypes.CouldNotFix
End Try
End Function
Public Shared Sub MergeTableIntoDataSet(tempTable As DataTable,
ds As DataSet,
preserveChanges As PreserveChangesTypes)
' merges temp table into dataset
'
' vars passed:
' tempTable - table to merge
' ds - data set to merge into
' preserveChanges - preserve changes option
Try
Select Case preserveChanges
Case PreserveChangesTypes.None
ds.Merge(tempTable)
Case PreserveChangesTypes.No
ds.Merge(tempTable, False, MissingSchemaAction.Ignore)
Case PreserveChangesTypes.Yes
ds.Merge(tempTable, True, MissingSchemaAction.Ignore)
End Select
Catch ex As Exception
_errorMessage = String.Format(efOther, ex.Message)
_errorCode = ex.HResult
End Try
End Sub
#End Region
#Region " Fix Constraint Exception "
Private Shared Function CheckNonLinkedTable(ChildTable As DataTable) As Integer
' checks the table with non linked rows, making sure it has:
' 1) there is a table
' 2) only 1 parent relation
' 3) parent table has rows
' 4) only 1 linked column in parent table
' 5) only 1 linked column in child table
'
' vars passed:
' ChildTable - table containing non linked rows
'
' returns:
' NoErrors - no errors
' teNoTableErr - no table
' nlNoParentRels - no parent relations
' nlMultiParentRels - multiple parent relations
' nlNoLinkedColumnsErr - no linked column in parent or child table
' nlMultiLinkedColumnsErr - multiple linked columns in parent ot child table
' teOtherErr - other error
Try
Dim ChildTableName As String = ChildTable.TableName
' make sure got only 1 parent relation
If ChildTable.ParentRelations.Count = 0 Then
_errorMessage = String.Format("No Parent Relation for table ""{0}"".", ChildTable.TableName)
_errorCode = nlNoParentRelsErr
Return nlNoParentRelsErr
End If
If ChildTable.ParentRelations.Count > 1 Then
_errorMessage = String.Format("Multiple Parent Relation for table ""{0}"".", ChildTable.TableName)
_errorCode = nlMultiParentRelsErr
Return nlMultiParentRelsErr
End If
' get parent table name
Dim ParentTableName As String = ChildTable.ParentRelations(0).ParentTable.TableName
' make sure only 1 parent column linked to only 1 child column
If ChildTable.ParentRelations(0).ChildKeyConstraint.RelatedColumns.Count = 0 Then
_errorMessage = String.Format("No Linked Column in parent table ""{0}"".", ParentTableName)
_errorCode = nlNoLinkedColumnsErr
Return nlNoLinkedColumnsErr
End If
If ChildTable.ParentRelations(0).ChildKeyConstraint.RelatedColumns.Count > 1 Then
_errorMessage = String.Format("Multiple Linked Columns in parent table ""{0}"".", ParentTableName)
_errorCode = nlNoLinkedColumnsErr
Return nlMultiLinkedColumnsErr
End If
If ChildTable.ParentRelations(0).ChildKeyConstraint.Columns.Count = 0 Then
EaTools.Tools.ShowErrorMessage(String.Format("No Linked Column in child table ""{0}"".", ChildTableName), etFixingConstraintErr)
Return nlNoLinkedColumnsErr
End If
If ChildTable.ParentRelations(0).ChildKeyConstraint.Columns.Count > 1 Then
_errorMessage = String.Format("Multiple Linked Columns in child table ""{0}"".", ChildTableName)
Return nlMultiLinkedColumnsErr
Return nlMultiLinkedColumnsErr
End If
Return NoErrors
Catch ex As Exception
_errorMessage = String.Format(efOther, ex.Message)
_errorCode = ex.HResult
Return teOtherErr
End Try
End Function
Private Shared Function CreateNonLinked(MySqlConn As MySqlConnection,
ChildTable As DataTable,
Optional CheckChildTable As Boolean = True) As Integer
' created the table "NonLinked" from non linked rows in aTable
'
' note: this function assumes:
' 1) only 1 parent relation, so can use zero index in aTable.ParentRelations
' 2) only one column from the parent table is linked to one column in the child table
'
' vars passed:
' MySqlConn - MySql connection
' ChildTable - table containing non linked rows
' CheckChildTable - if TRUE, then need to check child table
'
' returns:
' >= 0 # of rows inserted into NonLinked table
' teNoTableErr - no table
' nlNoParentRels - no parent relations
' nlMultiParentRels - multiple parent relations
' nlNoLinkedColumnsErr - no linked column in parent or child table
' nlMultiLinkedColumnsErr - multiple linked columns in parent ot child table
' <0 - SqlCommand.ExecuteNonQuery error value
' sqlNoTableName - no table name
' sqlErr - other error in sql
' sqlDropErr - other error in dropping table
' teOtherErr - other error
Try
Dim ChildTableName As String = String.Empty
Dim NlErr As Integer
If CheckChildTable Then ' if need to check if child table is OK
NlErr = CheckNonLinkedTable(ChildTable) ' check if table is ok to remove non linked rows
If NlErr <> NoErrors Then ' if got error in check
Return NlErr ' return error
End If
End If
If MySqlTools.TableExists(MySqlConn, NonLinkedTableName) = MySqlTools.ExistsTypes.Yes Then ' if got temp NonLinked table
NlErr = MySqlTools.DropTable(MySqlConn, NonLinkedTableName) ' delete temp table
If NlErr < 0 Then ' if error deleting table
Return NlErr ' return the error
End If
End If
ChildTableName = ChildTable.TableName
' CREATE TABLE NonLinked LIKE ChildTableName
Dim createSql As String = String.Format("CREATE TABLE {0} LIKE {1}", NonLinkedTableName, ChildTableName)
Dim createErr As Integer = MySqlTools.ExecuteNonQuery(MySqlConn, createSql) ' create non linked table like child
If createErr < 0 Then ' if had an error
Return createErr ' return error
End If
Dim ParentTableName As String = ChildTable.ParentRelations(0).ParentTable.TableName ' get parent table name
' INSERT INTO NonLinked (ColumnName1, ColumnName2, ...)
' SELECT ChildTableName.ColumnName1, ChildTableName.ColumnName2, ...
' FROM ChildTableName LEFT JOIN ParentTableName
' ON ChildTableName.ChildLinkColumnName = ParentTableName.ParentLinkColumnName
' WHERE ((ParentTableName.ParentLinkColumnName) Is Null)
Dim ColumnNamesStr As String = MySqlTools.GetColumnNames(MySqlConn, ChildTable, ChildTableName) ' get child columns as one string
If ColumnNamesStr = String.Empty Then ' if no child columns
Return teOtherErr ' exit now
End If
Dim justColNamesStr As String = ColumnNamesStr.Replace(ChildTableName & ".", "") ' get child cols w/o table name
Dim InsertSql As String = String.Format("INSERT INTO {0} ({1})", NonLinkedTableName, justColNamesStr)
Dim SelectSQL As String = String.Format(" SELECT {0}", ColumnNamesStr)
Dim ParentLinkColumnName As String =
ChildTable.ParentRelations(0).ChildKeyConstraint.RelatedColumns(0).ColumnName
Dim ChildLinkColumnName As String =
ChildTable.ParentRelations(0).ChildKeyConstraint.Columns(0).ColumnName
Dim FromSql As String = String.Format(" FROM {0} LEFT JOIN {1} ON {0}.{2} = {1}.{3}", ChildTableName, ParentTableName, ChildLinkColumnName, ParentLinkColumnName)
Dim WhereSql As String = String.Format(" WHERE (({0}.{1}) Is Null)", ParentTableName, ParentLinkColumnName)
' returns # of rows inserted or error value
Return MySqlTools.ExecuteNonQuery(MySqlConn, InsertSql & SelectSQL & FromSql & WhereSql)
Catch ex As Exception
_errorMessage = String.Format(efOther, ex.Message)
_errorCode = ex.HResult
Return teOtherErr
End Try
End Function
Private Shared Function DeleteNonLinkedRows(MySqlConn As MySqlConnection,
ChildTable As DataTable) As Integer
' Deletes non linked rows from a table. If no temp save table exists, then one is created.
' The data saved will be the non linked rows in ChildTable. The temp table will be named
' "ChildTableName" & "Nl". If temp table does exist, then the non linked rows will be inserted
' into the temp table
'
' note: this function assumes:
' 1) only 1 parent relation, so can use zero index in aTable.ParentRelations
' 2) only one column from the parent table is linked to one column in the child table
'
' vars passed:
' MySqlConn - MySql connection
' ChildTable - table to delete non linked rows from
'
' returns:
' NoErrors - no errors
' teNoTableErr - no table
' nlNoParentRels - no parent relations
' nlMultiParentRels - multiple parent relations
' nlNoLinkedColumnsErr - no linked column in parent or child table
' nlMultiLinkedColumnsErr - multiple linked columns in parent ot child table
' teParametersErr - no value for parameter
' <0 - SqlCommand.ExecuteNonQuery error value
' sqlErr- other error in sql
' sqlNoTableName - from or to table name not set
' sqlErr- other error in sql
' sqlMakeTableErr - error in make table sql
' sqlDropErr - other error in dropping table
' sqlRenameTableErr - other error in rename
' teOtherErr - other error
Const DeleteSQL As String = "DELETE "
Try
Dim ChildTableName As String = String.Empty
Dim NlErr As Integer = CheckNonLinkedTable(ChildTable) ' make sure table is ok to remove non linked rows
If NlErr <> NoErrors Then ' if not OK
Return NlErr ' return error
End If
ChildTableName = ChildTable.TableName
Dim ChildNlTableName As String = ChildTableName & NonLinkedAdded
' create the non linked rows table
Dim NlRowCount As Integer = CreateNonLinked(MySqlConn, ChildTable, False) ' create temp non linked table
If NlRowCount < 0 Then ' if got an error
Return NlRowCount ' return the error
End If
If NlRowCount = 0 Then ' if no non linked rows
_errorMessage = String.Format("No ""Non Linked"" rows found in table ""{0}"".", ChildTableName)
_errorCode = NoErrors
Return NoErrors ' return no errors
End If
If TableExists(MySqlConn, NonLinkedTableName) <> ExistsTypes.Yes Then ' if no NonLinked table, then exit now
_errorMessage = String.Format("Table ""{0}"". not found", NonLinkedTableName)
_errorCode = teNoTableErr
Return teNoTableErr ' return error
End If
' DELETE
' FROM ChildTableName
' WHERE ChildKeyColumnName IN (SELECT ChildKeyColumnName FROM NonLinked)
Dim FromSql As String = " FROM " & ChildTableName
Dim ChildKeyColumnName As String = ChildTable.PrimaryKey(0).ColumnName
Dim WhereSql As String = String.Format(" WHERE {0} IN (SELECT {0} FROM {1})", ChildKeyColumnName, NonLinkedTableName)
NlRowCount = MySqlTools.ExecuteNonQuery(MySqlConn, DeleteSQL & FromSql & WhereSql)
If NlRowCount < 0 Then ' if error deleting non linked rows
Return NlRowCount ' return error
End If
' save the non linked rows
Dim GotTempTable As ExistsTypes = TableExists(MySqlConn, ChildNlTableName) ' see if got temp table
If GotTempTable = ExistsTypes.No Then ' if no temp table yet
NlErr = RenameTable(MySqlConn, NonLinkedTableName, ChildNlTableName) ' rename NonLinked to ChildNL
ElseIf GotTempTable = ExistsTypes.Yes Then ' else got child nl table
NlErr = InsertNonLinkedRows(MySqlConn, ChildTable, ChildNlTableName) ' save nl rows to child nl
Else
NlErr = teOtherErr
End If
If NlErr < 0 Then ' if got an error in rename or Insert
Return NlErr ' return the error
End If
If TableExists(MySqlConn, NonLinkedTableName) = ExistsTypes.Yes Then ' if got temp NonLinked table, need to delete it
NlErr = DropTable(MySqlConn, NonLinkedTableName) ' delete temp table
If NlErr < 0 Then ' if error deleting table
Return NlErr ' return the error
End If
End If
Return NoErrors ' if got here, then deleted non linked rows
Catch ex As Exception
_errorMessage = String.Format(efOther, ex.Message)
_errorCode = ex.HResult
Return teOtherErr
End Try
End Function
Public Shared Function FixConstraintException(MySqlConn As MySqlConnection,
Table As DataTable) As Integer
' fixes problem in for non linked rows in table when filling table
' non linked rows are moved from table and saved in non linked rows table
'
' vars passed:
' MySqlConn - MySql connection
' Table - table to fix
'
' returns:
' NoErrors - table had non linked rows moved to separate table
' teConcurrencyErr -
' - table is a temp table
' - currently fixing non linked rows for another table
' - could not move non linked rows
' teNoParentRows - no parent table
' teOtherErr - something went wrong
Try
_fixedMessage = String.Empty
If MySqlTools.TableExists(MySqlConn, Table.TableName) <> MySqlTools.ExistsTypes.Yes Then
_errorMessage = String.Format("Data table ""{0}"" has unlinked rows or duplicate key values. It is a temporary table and cannot be fixed. Table was not loaded.", Table.TableName)
_errorCode = teConcurrencyErr
Return teConcurrencyErr
End If
If FixingNonLinkedRows Then ' if current fixing
_errorMessage = "Already fixing Non Linked rows, cannot fix other table at this time."
_errorCode = teConcurrencyErr
Return teConcurrencyErr
End If
Dim ParentTableName As String = String.Empty
If ParentRowCount(Table, ParentTableName) = 0 Then ' if no parent table rows
_errorMessage = String.Format("Data table ""{0}"" is a child table of ""{1}"". Parent table ""{1}"" has no rows.", Table.TableName, ParentTableName)
_errorCode = teNoParentRows
Return teNoParentRows
End If
FixingNonLinkedRows = True ' now fixing table
If DeleteNonLinkedRows(MySqlConn, Table) <> NoErrors Then ' if did not delete non linked rows
Return teConstraintErr
End If
_fixedMessage = String.Format("Table ""{0}"" fixed. Non linked rows have been moved to table ""{0}{1}"".", Table.TableName, NonLinkedAdded)
Return NoErrors
Catch ex As Exception
_errorMessage = String.Format(efOther, ex.Message)
_errorCode = teOtherErr
Return teOtherErr
End Try
End Function
Private Shared Function InsertNonLinkedRows(MySqlConn As MySqlConnection,
ChildTable As DataTable,
ChildNlTableName As String) As Integer
' inserts the rows from NonLinked table in to temp child table
'
' vars passed:
' ChildTable - table to delete non linked rows from
' ChildNlTableName - name of child temp table to save non linked rows
'
' returns:
' NoErrors - no errors
' teNoTableErr - no table
' teActualColumnsErr - could not get actual columns for child table
' <0 - SqlCommand.ExecuteNonQuery error value
' sqlErr- other error in sql
' teOtherErr - other error
Try
If ChildTable Is Nothing Then ' if no value in parameter
Return teNoTableErr ' return no table error
End If
If ChildNlTableName = String.Empty Then ' if no value in parameter
Return teNoTableErr ' return no table error
End If
' INSERT INTO ChildNlTableName ( ColumnName1, ColumnName2, ... )
' SELECT NonLinked.ColumnName1, NonLinked.ColumnName2, ...
' FROM NonLinked
Dim ColumnNamesStr As String = MySqlTools.GetColumnNames(MySqlConn, ChildTable, NonLinkedTableName) ' get child columns as one string
If ColumnNamesStr = String.Empty Then ' if no child columns
Return teActualColumnsErr ' exit now
End If
Dim justColNamesStr As String = ColumnNamesStr.Replace(NonLinkedTableName & ".", "") ' get child cols w/o table name
Dim InsertSql As String = String.Format("INSERT INTO {0} ({1}) ", ChildNlTableName, justColNamesStr)
Dim SelectSql As String = String.Format("SELECT {0} ", ColumnNamesStr)
Dim FromSql As String = "FROM " & NonLinkedTableName
Return MySqlTools.ExecuteNonQuery(MySqlConn, InsertSql & SelectSql & FromSql)
Catch ex As Exception
_errorMessage = String.Format(efOther, ex.Message)
_errorCode = teOtherErr
Return teOtherErr
End Try
End Function
Private Shared Function ParentRowCount(ChildTable As DataTable,
ByRef ParentTableName As String) As Integer
' gets the # of rows in the parent table in the data set (filled rows in the parent table)
'
' vars passed:
' ChildTable - child data table with check parent table to check
' ParentTableName - child data table's parent table name (pass String.Empty, will be set to child data table's parent table name)
'
' returns:
' >=0 - # of rows for parent table
' teNoTableErr - no table
' nlNoParentRels - no parent relations
' nlMultiParentRels - multiple parent relations
' teOtherErr - something went wrong
Try
Dim ChildTableName As String = String.Empty
If ChildTable Is Nothing Then
Return teNoTableErr
End If
ChildTableName = ChildTable.TableName
' make sure got only 1 parent relation
If ChildTable.ParentRelations.Count = 0 Then
EaTools.Tools.ShowErrorMessage(String.Format("No Parent Relation for table ""{0}"".", ChildTableName), etFixingConstraintErr)
Return nlNoParentRelsErr
End If
If ChildTable.ParentRelations.Count > 1 Then
EaTools.Tools.ShowErrorMessage(String.Format("Multiple Parent Relation for table ""{0}"".", ChildTableName), etFixingConstraintErr)
Return nlMultiParentRelsErr
End If
ParentTableName = ChildTable.ParentRelations(0).ParentTable.TableName ' set parent table name
Return ChildTable.ParentRelations(0).ParentTable.Rows.Count ' return # rows in the parent datatable
Catch ex As Exception
_errorMessage = String.Format(efOther, ex.Message)
_errorCode = ex.HResult
Return teOtherErr
End Try
End Function
#End Region
End Class