Skip to content

Commit

Permalink
#81 완료
Browse files Browse the repository at this point in the history
[+] 정규 표현식 검색 시 기본적으로 맨 앞에 '^'와 맨 뒤에 '$' 연산자가 붙어 있도록 처리
[/] 중복 단어 제거 쿼리에서 하드코딩되어 있던 열 이름을 데이터베이스 상수 레퍼런스하도록 변경
[+] SQLite 데이터베이스 가져오기 작업 시 데이터베이스를 열 수 없으면 작업을 즉시 중단하도록 변경
[+] 정규 표현식 단어 제거 옵션 구현 완료 #81
  • Loading branch information
hsheric0210 committed Dec 3, 2023
1 parent ad11461 commit c0990b9
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 319 deletions.
381 changes: 71 additions & 310 deletions AutoKkutuGui/DatabaseManagement.xaml

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions AutoKkutuGui/DatabaseManagement.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private void BatchWordJob(string content, BatchJobOptions options, WordFlags fla

var wordList = content.Trim().Split(Environment.NewLine.ToCharArray());

((BatchWordJob)(options.HasFlag(BatchJobOptions.Remove) ? new BatchWordDeletionJob(autoKkutu.Database) : new BatchWordAdditionJob(autoKkutu.NodeManager, autoKkutu.Browser, flags, options.HasFlag(BatchJobOptions.Verify)))).Execute(wordList);
((BatchWordJob)(options.HasFlag(BatchJobOptions.Remove) ? new BatchWordDeletionJob(autoKkutu.Database, regexp: options.HasFlag(BatchJobOptions.Regexp)) : new BatchWordAdditionJob(autoKkutu.NodeManager, autoKkutu.Browser, flags, options.HasFlag(BatchJobOptions.Verify)))).Execute(wordList);
}

private void Batch_Submit_Click(object sender, RoutedEventArgs e) => BatchWordJob(Batch_Input.Text, GetBatchWordJobFlags(), GetBatchAddWordDatabaseAttributes());
Expand Down Expand Up @@ -161,10 +161,12 @@ private BatchJobOptions GetBatchWordJobFlags()
{
var mode = BatchJobOptions.None;

// Remove
if (Batch_Remove.IsChecked ?? false)
if (Batch_Mode_Remove.IsSelected || Batch_Mode_Remove_Regexp.IsSelected)
mode |= BatchJobOptions.Remove;

if (Batch_Mode_Remove_Regexp.IsSelected)
mode |= BatchJobOptions.Regexp;

// Verify-before-Add
if (Batch_Verify.IsChecked ?? false)
mode |= BatchJobOptions.Verify;
Expand Down Expand Up @@ -230,6 +232,7 @@ private enum BatchJobOptions
{
None = 0 << 0,
Remove = 1 << 0,
Verify = 1 << 1
Verify = 1 << 1,
Regexp = 1 << 2
}
}
3 changes: 3 additions & 0 deletions AutoKkutuLib.Sqlite/Database/Sqlite/SqliteDatabaseHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ public static void LoadFromExternalSQLite(DbConnectionBase targetDatabase, strin
{
var connection = SqliteDbConnection.Create(externalSQLiteFilePath);
if (connection == null)
{
LibLogger.Error(nameof(SqliteDatabaseHelper), "Failed to open SQLite connection");
return;
}

var args = new SQLiteImportArgs { destination = targetDatabase, source = connection };
var WordCount = LogImportProcess("Import words", () => ImportWordsFromExternalSQLite(args));
Expand Down
9 changes: 7 additions & 2 deletions AutoKkutuLib/Database/Jobs/Word/BatchWordDeletionJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ namespace AutoKkutuLib.Database.Jobs.Word;
public sealed class BatchWordDeletionJob : BatchWordJob
{
private readonly bool transactioned;
private readonly bool regexp;

public BatchWordDeletionJob(DbConnectionBase dbConnection, bool transactioned = true) : base(dbConnection) => this.transactioned = transactioned;
public BatchWordDeletionJob(DbConnectionBase dbConnection, bool transactioned = true, bool regexp = false) : base(dbConnection)
{
this.transactioned = transactioned;
this.regexp = regexp;
}

public override WordCount Execute(IEnumerable<string> wordList)
{
Expand Down Expand Up @@ -49,7 +54,7 @@ private bool RemoveSingleWord(WordDeletionQuery query, string word)
try
{
LibLogger.Info<BatchWordAdditionJob>("Removing {word} from database...", word);
return query.Execute(word) > 0;
return query.Execute(word, regexp) > 0;
}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion AutoKkutuLib/Database/Sql/Query/DeduplicationQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public DeduplicationQuery(DbConnectionBase connection) : base(connection)
public override int Execute()
{
LibLogger.Debug<DeduplicationQuery>($"Deduplicating the table {DatabaseConstants.WordTableName}.");
return Connection.Execute($"DELETE FROM {DatabaseConstants.WordTableName} WHERE seq IN (SELECT seq FROM (SELECT seq, ROW_NUMBER() OVER w as rnum FROM {DatabaseConstants.WordTableName} WINDOW w AS (PARTITION BY word ORDER BY seq)) t WHERE t.rnum > 1);");
return Connection.Execute($"DELETE FROM {DatabaseConstants.WordTableName} WHERE {DatabaseConstants.SequenceColumnName} IN (SELECT {DatabaseConstants.SequenceColumnName} FROM (SELECT {DatabaseConstants.SequenceColumnName}, ROW_NUMBER() OVER w as rnum FROM {DatabaseConstants.WordTableName} WINDOW w AS (PARTITION BY {DatabaseConstants.WordColumnName} ORDER BY {DatabaseConstants.SequenceColumnName})) t WHERE t.rnum > 1);");
}
}
4 changes: 3 additions & 1 deletion AutoKkutuLib/Database/Sql/Query/FindWordQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ private FindQuery CreateQuery(PathDetails parameter)
if (word.Regexp)
{
// 정규 표현식 검색
param.Add("@PrimaryWord", "(?i)" + word.Char); // '(?i) for Case-insensitive match - https://stackoverflow.com/a/43636
// '(?i) for Case-insensitive match - https://stackoverflow.com/a/43636
// '^' means start of the string, '$' means end of the string
param.Add("@PrimaryWord", "(?i)^" + word.Char + '$');

// SQLite: https://github.com/nalgeon/sqlean/blob/main/docs/regexp.md
// PostgreSQL: https://www.postgresql.org/docs/current/functions-matching.html
Expand Down
2 changes: 1 addition & 1 deletion AutoKkutuLib/Database/Sql/Query/WordDeletionQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public override int Execute()
string query;
if (Regexp)
{
Word = "(?i)" + Word;
Word = "(?i)^" + Word + '$';
query = $"DELETE FROM {DatabaseConstants.WordTableName} WHERE {DatabaseConstants.WordColumnName} REGEXP @Word;";
}
else
Expand Down

0 comments on commit c0990b9

Please sign in to comment.