Exemple #1
0
        protected override void ProcessRecord()
        {
            if (AnotherFileId.HasValue && !FileId.HasValue)
            {
                FileId        = AnotherFileId.Value;
                AnotherFileId = null;
            }

            var    connection  = DatabaseConnection.Current;
            string commandPart = " [Id],[File1Id],[File2Id],[DifferenceDegree],[IgnoredMode] from [SimilarFile]";

            using (var command = new SqlCommand()
            {
                CommandTimeout = 0
            })
            {
                command.Connection = connection;
                if (Top.HasValue)
                {
                    command.CommandText = "SELECT TOP " + Top.Value.ToString() + commandPart;
                }
                else
                {
                    command.CommandText = "SELECT" + commandPart;
                }


                WhereCauseBuilder whereCauseBuilder = new WhereCauseBuilder(command.Parameters);

                if (FileId.HasValue)
                {
                    if (AnotherFileId.HasValue)
                    {
                        whereCauseBuilder.AddPlainCause("(([File1Id] = @File1Id and [File2Id] = @File2Id) or ([File2Id] = @File1Id and [File1Id] = @File2Id))");
                        command.Parameters.Add(new SqlParameter("@File1Id", System.Data.SqlDbType.UniqueIdentifier)
                        {
                            Value = FileId.Value
                        });
                        command.Parameters.Add(new SqlParameter("@File2Id", System.Data.SqlDbType.UniqueIdentifier)
                        {
                            Value = AnotherFileId.Value
                        });
                    }
                    else
                    {
                        whereCauseBuilder.AddPlainCause("([File1Id] = @FileId or [File2Id] = @FileId)");
                        command.Parameters.Add(new SqlParameter("@FileId", System.Data.SqlDbType.UniqueIdentifier)
                        {
                            Value = FileId.Value
                        });
                    }
                }

                whereCauseBuilder.AddRealComparingCause("DifferenceDegree", DifferenceDegree, DifferenceDegreeGreaterOrEqual, DifferenceDegreeLessOrEqual);

                List <int> ignoredModes = new List <int>();
                if (IncludesEffective.IsPresent)
                {
                    ignoredModes.Add(0);
                }
                if (IncludesHiddenButConnected.IsPresent)
                {
                    ignoredModes.Add(1);
                }
                if (IncludesHiddenAndDisconnected.IsPresent)
                {
                    ignoredModes.Add(2);
                }
                if (ignoredModes.Count != 3)
                {
                    if (ignoredModes.Count == 0)
                    {
                        ignoredModes.Add(0);
                    }
                    whereCauseBuilder.AddIntInRangeCause("IgnoredMode", ignoredModes);
                }

                command.CommandText += whereCauseBuilder.ToFullWhereCommand();

                if (OrdersByDifferenceDegree.IsPresent)
                {
                    command.CommandText += " order by [DifferenceDegree]";
                }

                List <ImageStoreSimilarFile> result = new List <ImageStoreSimilarFile>();

                using (var reader = command.ExecuteReader(System.Data.CommandBehavior.SequentialAccess))
                {
                    while (reader.Read())
                    {
                        ImageStoreSimilarFile line = new ImageStoreSimilarFile((Guid)reader[0], (Guid)reader[1], (Guid)reader[2], (float)reader[3])
                        {
                            IgnoredModeCode = (int)reader[4]
                        };
                        result.Add(line);
                    }
                    reader.Close();
                }

                WriteObject(result);
            }
        }
        protected override void ProcessRecord()
        {
            var connection = DatabaseConnection.Current;

            using (var command = new SqlCommand(" [Id],[FolderId],[Path],[FileName],[ExtensionId],[ImageHash],[Sha1Hash],[FileSize],[FileState],[ImageComparedThreshold] from [File]"))
            {
                command.Connection     = connection;
                command.CommandTimeout = 0;
                if (Top.HasValue)
                {
                    command.CommandText = "SELECT TOP " + Top.Value.ToString() + command.CommandText;
                }
                else
                {
                    command.CommandText = "SELECT" + command.CommandText;
                }

                WhereCauseBuilder whereCauseBuilder = new WhereCauseBuilder(command.Parameters);
                whereCauseBuilder.AddUniqueIdentifierComparingCause("FolderId", FolderId);
                whereCauseBuilder.AddStringComparingCause("Path", Path, PathPropertyComparingModes);
                whereCauseBuilder.AddStringComparingCause("FileName", FileName, FileNamePropertyComparingModes);
                whereCauseBuilder.AddUniqueIdentifierComparingCause("ExtensionId", ExtensionId);
                whereCauseBuilder.AddBinaryComparingCause("ImageHash", ImageHash, ImageHashIsNull.IsPresent, 40);
                whereCauseBuilder.AddBinaryComparingCause("Sha1Hash", Sha1Hash, Sha1HashIsNull.IsPresent, 20);
                whereCauseBuilder.AddIntComparingCause("FileSize", FileSize, FileSizeGreaterOrEqual, FileSizeLessOrEqual);

                List <int> fileStates = new List <int>();
                if (IncludesNewFile.IsPresent)
                {
                    fileStates.Add(0);
                }
                if (IncludesNotImage.IsPresent)
                {
                    fileStates.Add(1);
                }
                if (IncludesNotReadable.IsPresent)
                {
                    fileStates.Add(2);
                }
                if (IncludesSizeZero.IsPresent)
                {
                    fileStates.Add(254);
                }
                if (IncludesComputed.IsPresent)
                {
                    fileStates.Add(255);
                }
                if (fileStates.Count != 5)
                {
                    whereCauseBuilder.AddIntInRangeCause("FileState", fileStates);
                }

                whereCauseBuilder.AddRealComparingCause("ImageComparedThreshold", ImageComparedThreshold, ImageComparedThresholdGreaterOrEqual, ImageComparedThresholdLessOrEqual);

                command.CommandText += whereCauseBuilder.ToFullWhereCommand();

                command.CommandText += " order by [FolderId], [Path], [FileName], [ExtensionId], [FileState]";

                List <ImageStoreFile> result = new List <ImageStoreFile>();

                using (var reader = command.ExecuteReader(System.Data.CommandBehavior.SequentialAccess))
                {
                    while (reader.Read())
                    {
                        ImageStoreFile line = new ImageStoreFile((Guid)reader[0], (Guid)reader[1], (string)reader[2], (string)reader[3], (Guid)reader[4])
                        {
                            ImageHash              = DBNullableReader.ConvertFromReferenceType <byte[]>(reader[5]),
                            Sha1Hash               = DBNullableReader.ConvertFromReferenceType <byte[]>(reader[6]),
                            FileSize               = (int)reader[7],
                            FileStateCode          = (int)reader[8],
                            ImageComparedThreshold = (float)reader[9]
                        };
                        result.Add(line);
                    }
                    reader.Close();
                }

                WriteObject(result);
            }
        }