Exemplo n.º 1
0
 public static DataTable Select(this IDAC dac, DataRow row)
 {
     return(dac.Select(
                row.Table.TableName,
                columns: (row.Table.Columns.Cast <DataColumn>().Select(c => c.ColumnName)).ToArray(),
                columnMatches: (from key in row.Table.PrimaryKey select new ColumnValue(key.ColumnName, row[key.ColumnName])).ToArray()
                ));
 }
Exemplo n.º 2
0
 protected virtual void PrintTableData(IDAC dac, string header, string tableName)
 {
     Console.WriteLine(header);
     foreach (var row in dac.Select(tableName).Rows.Cast <DataRow>())
     {
         Console.WriteLine("\t{0}".FormatWith(row.ItemArray.ToDelimittedString(",\t", "NULL")));
     }
     Console.WriteLine();
 }
Exemplo n.º 3
0
        public static long Count(this IDAC dac, string tableName, IEnumerable <ColumnValue> columnMatches = null, string whereClause = null)
        {
            var table = dac.Select(tableName, columns: new[] { "COUNT(1)" }, columnMatches: columnMatches, whereClause: whereClause);

            if (table.Rows.Count == 0)
            {
                return(0);
            }
            return(table.Rows[0].Get <long>(0));
        }
Exemplo n.º 4
0
        private void AssertRowsInternal(IDAC dac, string tableName, IEnumerable <IEnumerable <object> > expectedRows, bool anyOrder)
        {
            var tableRows = dac.Select(tableName).Rows.Cast <DataRow>().Select(r => r.ItemArray as IEnumerable <object>);

            if (anyOrder)
            {
                expectedRows = expectedRows.OrderByAll();
                tableRows    = tableRows.OrderByAll();
            }
            NUnitTool.AssertSame2DArrays(expectedRows, tableRows, tableName);
        }
Exemplo n.º 5
0
 public static async Task <DataTable> SelectAsync(this IDAC dac, string tableName, IEnumerable <string> columns = null, bool distinct = false, int?limit = null, int?offset = null, IEnumerable <ColumnValue> columnMatches = null, string whereClause = null, string orderByClause = null)
 {
     return(await Task.Run(() => dac.Select(tableName, columns, distinct, limit, offset, columnMatches, whereClause, orderByClause)));
 }
Exemplo n.º 6
0
 public virtual DataTable Select(string tableName, IEnumerable <string> columns = null, bool distinct = false, int?limit = null, int?offset = null, IEnumerable <ColumnValue> columnMatches = null, string whereClause = null, string orderByClause = null)
 {
     return(DecoratedDAC.Select(tableName, columns: columns, distinct: distinct, limit: limit, offset: offset, columnMatches: columnMatches, whereClause: whereClause, orderByClause: orderByClause));
 }
Exemplo n.º 7
0
        private void AssertSameTableDataInternal(IDAC source, IDAC dest, string tableName, bool primaryKeyMustMatch)
        {
            var sourceTableSchema = source.GetSchemaCached()[tableName];
            var destTableSchema   = dest.GetSchemaCached()[tableName];

            Assert.AreNotEqual(DBKeyType.None, sourceTableSchema.PrimaryKey.KeyType, "Tables without primary keys are not supported");
            Assert.AreEqual(sourceTableSchema.PrimaryKey.KeyType, destTableSchema.PrimaryKey.KeyType, "Table primary key types do not match");

            var sourceData = source.Select(tableName).Rows.Cast <DataRow>().Select(r => r.ItemArray.Select(Object.SanitizeObject).ToArray()).ToArray();
            var destData   = dest.Select(tableName).Rows.Cast <DataRow>().Select(r => r.ItemArray.Select(Object.SanitizeObject).ToArray()).ToArray();

            var primaryKeysNotInDest   = new IEnumerable <object> [0];
            var primaryKeysNotInSource = new IEnumerable <object> [0];

            // Validate data by primary keys
            if (primaryKeyMustMatch)
            {
                // Gather data into primary key look up
                var sourceDataDict = sourceTableSchema.ConvertDataToMultiKeyDictionary(sourceData);
                var destDataDict   = destTableSchema.ConvertDataToMultiKeyDictionary(destData);

                // Validate that primary keys map exactly
                primaryKeysNotInDest   = sourceDataDict.Keys.Except(destDataDict.Keys, new EnumerableSequenceEqualComparer <object>()).ToArray();
                primaryKeysNotInSource = destDataDict.Keys.Except(sourceDataDict.Keys, new EnumerableSequenceEqualComparer <object>()).ToArray();

                // Print data missing from dest (by PK)
                if (primaryKeysNotInDest.Any())
                {
                    Console.WriteLine("Primary key missing from dest:");
                    foreach (var key in primaryKeysNotInDest.Select(k => k.ToArray()))
                    {
                        Console.WriteLine("\tPK:({0}) Data:{1}".FormatWith(key.ToDelimittedString(", "), sourceDataDict[key].ToDelimittedString(", ")));
                    }
                }
                Console.WriteLine();

                // Print data missing from source (by PK)
                if (primaryKeysNotInSource.Any())
                {
                    Console.WriteLine("Primary key missing from source:");
                    foreach (var key in primaryKeysNotInSource.Select(k => k.ToArray()))
                    {
                        Console.WriteLine("\tPK:({0}) Data:{1}".FormatWith(key.ToDelimittedString(", "), sourceDataDict[key].ToDelimittedString(", ")));
                    }
                }
                Console.WriteLine();
            }

            var primaryKeyColCount  = sourceTableSchema.PrimaryKeyColumns.Length;
            var sourceDataToCompare = primaryKeyMustMatch ? sourceData : sourceData.Select(rowArr => rowArr.Skip(primaryKeyColCount).ToArray()).ToArray();
            var destDataToCompare   = primaryKeyMustMatch ? destData : destData.Select(rowArr => rowArr.Skip(primaryKeyColCount).ToArray()).ToArray();

            // print missing data
            var rowsNotInDestByValue   = sourceDataToCompare.Except(destDataToCompare, new EnumerableSequenceEqualComparer <object>()).ToArray();
            var rowsNotInSourceByValue = destDataToCompare.Except(sourceDataToCompare, new EnumerableSequenceEqualComparer <object>()).ToArray();

            // Print data missing from dest
            if (rowsNotInDestByValue.Any())
            {
                Console.WriteLine(NUnitTool.Convert2DArrayToString("Data missing from Dest", rowsNotInDestByValue));
            }
            Console.WriteLine();

            // Print data missing from source
            if (rowsNotInSourceByValue.Any())
            {
                Console.WriteLine(NUnitTool.Convert2DArrayToString("Data missing from Source", rowsNotInSourceByValue));
            }
            Console.WriteLine();

            // Assert primary keys are present in both
            if (primaryKeyMustMatch)
            {
                NUnitTool.IsEmpty(primaryKeysNotInDest, "Dest missing primary keys");
                NUnitTool.IsEmpty(primaryKeysNotInSource, "Source missing primary keys");
            }

            NUnitTool.IsEmpty(rowsNotInDestByValue, "Dest missing data");
            NUnitTool.IsEmpty(rowsNotInSourceByValue, "Source missing data");
        }