Skip to content

Commit 8f55357

Browse files
committed
remove unused functions. Add some more tests.
1 parent 8c96999 commit 8f55357

5 files changed

Lines changed: 93 additions & 77 deletions

File tree

SQLitePCL.pretty.Orm/DatabaseConnection.Tables.cs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,6 @@ namespace SQLitePCL.pretty.Orm
2525
{
2626
internal static partial class DatabaseConnection
2727
{
28-
/*
29-
/// <summary>
30-
/// Renames the table;
31-
/// </summary>
32-
/// <param name="This">The database connection.</param>
33-
/// <param name="table">The table name.</param>
34-
/// <param name="newName">The new table name.</param>
35-
/// <seealso href="https://www.sqlite.org/lang_altertable.html"/>
36-
internal static void Rename(this IDatabaseConnection This, string table, string newName)
37-
{
38-
This.Execute(SQLBuilder.AlterTableRename(table, newName));
39-
}*/
40-
41-
/// <summary>
42-
/// Drops the table.
43-
/// </summary>
44-
/// <param name="This">The database connection.</param>
45-
/// <param name="table">The table name.</param>
46-
/// <seealso href="https://www.sqlite.org/lang_droptable.html"/>
47-
internal static void DropTable(this IDatabaseConnection This, string table)
48-
{
49-
This.Execute(SQLBuilder.DropTable(table));
50-
}
51-
5228
/// <summary>
5329
/// Drops the table if it exists. Otherwise this is a no-op.
5430
/// </summary>

SQLitePCL.pretty.Orm/SQLBuilder.cs

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,14 @@ namespace SQLitePCL.pretty.Orm
66
{
77
internal static class SQLBuilder
88
{
9-
internal const string ReIndex = "REINDEX";
10-
11-
internal static string AlterTableRename(string table, string newName)
12-
{
13-
return string.Format("ALTER TABLE \"{0}\" RENAME TO \"{1}\"", table, newName);
14-
}
15-
169
internal static string DeleteAll(string tableName)
1710
{
1811
return string.Format("DELETE FROM \"{0}\"", tableName);
1912
}
2013

21-
internal static string DropTable(string tableName)
22-
{
23-
return string.Format("DROP TABLE \"{0}\"", tableName);
24-
}
25-
2614
internal static string DropTableIfExists(string tableName)
2715
{
28-
return string.Format("DROP TABLE If EXISTS \"{0}\"", tableName);
16+
return string.Format("DROP TABLE IF EXISTS \"{0}\"", tableName);
2917
}
3018

3119
internal static string GetTableInfo (string tableName)
@@ -53,17 +41,6 @@ internal static string DeleteUsingPrimaryKey(string tableName, string pkColumn)
5341
return string.Format("DELETE FROM \"{0}\" WHERE \"{1}\" = ?", tableName, pkColumn);
5442
}
5543

56-
internal static string Insert(string tableName, IEnumerable<string> columns)
57-
{
58-
return string.Format(
59-
"INSERT INTO \"{0}\" ({1}) VALUES ({2})",
60-
tableName,
61-
string.Join(",", columns.Select(x => "\"" + x + "\"")),
62-
63-
// FIXME: Might need to quote this for some cases. Test!!!
64-
string.Join(",", columns.Select(x => ":" + x)));
65-
}
66-
6744
internal static string InsertOrReplace(string tableName, IEnumerable<string> columns)
6845
{
6946
return string.Format(
@@ -81,21 +58,6 @@ internal static string CreateIndex(string indexName, string tableName, IEnumerab
8158
return String.Format(sqlFormat, tableName, string.Join ("\", \"", columnNames), unique ? "UNIQUE" : "", indexName);
8259
}
8360

84-
internal static string CreateIndex(string indexName, string tableName, string columnName, bool unique)
85-
{
86-
return CreateIndex(indexName, tableName, new string[] { columnName }, unique);
87-
}
88-
89-
internal static string CreateIndex(string tableName, string columnName, bool unique)
90-
{
91-
return CreateIndex(NameIndex(tableName, new String[] { columnName}), tableName, columnName, unique);
92-
}
93-
94-
internal static string CreateIndex(string tableName, IEnumerable<string> columnNames, bool unique)
95-
{
96-
return CreateIndex(NameIndex(tableName, columnNames), tableName, columnNames, unique);
97-
}
98-
9961
internal static string NameIndex(string tableName, IEnumerable<string> columnNames)
10062
{
10163
return tableName + "_" + string.Join ("_", columnNames);
@@ -116,11 +78,6 @@ internal static string IndexInfo(string indexName)
11678
return string.Format("PRAGMA INDEX_INFO (\"{0}\")", indexName);
11779
}
11880

119-
internal static string ReIndexWithName(string name)
120-
{
121-
return "REINDEX " + name;
122-
}
123-
12481
internal static string CreateTableIfNotExists(string tableName, CreateFlags createFlags, IReadOnlyDictionary<string, ColumnMapping> columns)
12582
{
12683
bool fts3 = (createFlags & CreateFlags.FullTextSearch3) != 0;

SQLitePCL.pretty.Orm/TableMapping.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public static TableQuery<T> CreateQuery<T>(this ITableMapping<T> This)
5353
{
5454
return new TableQuery<T>(This, "*", null, new List<Ordering>(), null, null);
5555
}
56-
56+
57+
5758
public static void InitTable<T>(this IDatabaseConnection This, ITableMapping<T> tableMapping)
5859
{
5960
This.RunInTransaction(_ =>
@@ -114,19 +115,19 @@ private static string PrimaryKeyColumn(this ITableMapping This)
114115
mapping.Columns.Where(x => x.Value.Metadata.IsPrimaryKeyPart).Select(x => x.Key).First());
115116
}
116117

117-
public static void DropTable<T>(this IDatabaseConnection This, ITableMapping<T> tableMapping)
118+
public static void DropTableIfExists<T>(this IDatabaseConnection This, ITableMapping<T> tableMapping)
118119
{
119-
This.DropTable(tableMapping.TableName);
120+
This.DropTableIfExists(tableMapping.TableName);
120121
}
121122

122-
public static Task DropTableAsync<T>(this IAsyncDatabaseConnection This, ITableMapping<T> tableMapping, CancellationToken ct)
123+
public static Task DropTableIfExistsAsync<T>(this IAsyncDatabaseConnection This, ITableMapping<T> tableMapping, CancellationToken ct)
123124
{
124-
return This.Use((db, _) => db.DropTable(tableMapping), ct);
125+
return This.Use((db, _) => db.DropTableIfExists(tableMapping), ct);
125126
}
126127

127-
public static Task DropTableAsync<T>(this IAsyncDatabaseConnection This, ITableMapping<T> tableMapping)
128+
public static Task DropTableIfExistsAsync<T>(this IAsyncDatabaseConnection This, ITableMapping<T> tableMapping)
128129
{
129-
return This.DropTableAsync(tableMapping, CancellationToken.None);
130+
return This.DropTableIfExistsAsync(tableMapping, CancellationToken.None);
130131
}
131132

132133
public static ITableMapping<T> Create<T>()

SQLitePCL.pretty.tests/OrmTests/TableMappingTest.Async.cs

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Collections.Generic;
44
using System.IO;
55
using System.Linq;
6+
using System.Reactive.Linq;
7+
using System.Threading.Tasks;
68

79
using SQLitePCL.pretty.Orm;
810
using SQLitePCL.pretty.Orm.Attributes;
@@ -13,7 +15,79 @@ namespace SQLitePCL.pretty.tests
1315
[TestFixture]
1416
public partial class TableMappingTests
1517
{
16-
18+
[Test]
19+
public async Task TestDeleteAllAsync()
20+
{
21+
var table = TableMapping.Create<TestObject>(
22+
() => testObjectBuilder.Value,
23+
o => ((TestObject.Builder)o).Build());
24+
25+
using (var db = SQLite3.OpenInMemory().AsAsyncDatabaseConnection())
26+
{
27+
28+
var objects = new List<TestObject>()
29+
{
30+
new TestObject.Builder() { Value = "Hello1" }.Build(),
31+
new TestObject.Builder() { Value = "Hello2" }.Build(),
32+
new TestObject.Builder() { Value = "Hello3" }.Build()
33+
};
34+
35+
await db.InitTableAsync(table);
36+
var notDeleted = await db.Use(x => x.InsertOrReplace(table, new TestObject.Builder() { Value = "NotDeleted" }.Build()));
37+
var inserted = await db.InsertOrReplaceAllAsync(table, objects);
38+
var deleted = await db.DeleteAllAsync(table, inserted.Values.Select(x => x.Id.Value));
39+
CollectionAssert.AreEquivalent(inserted.Values, deleted.Values);
40+
41+
var found = await db.FindAllAsync(table, inserted.Values.Select(x => x.Id.Value));
42+
Assert.IsEmpty(found);
43+
44+
await db.Use(x =>
45+
{
46+
TestObject found2;
47+
48+
if (x.TryFind(table, notDeleted.Id.Value, out found2))
49+
{
50+
Assert.AreEqual(found2, notDeleted);
51+
}
52+
});
53+
54+
await db.DeleteAllRowsAsync(table);
55+
56+
await db.Use(x =>
57+
{
58+
TestObject notFound;
59+
Assert.IsFalse(x.TryFind(table, notDeleted.Id.Value, out notFound));
60+
});
61+
}
62+
}
63+
64+
[Test]
65+
public async Task TestDropTableAsync()
66+
{
67+
var table = TableMapping.Create<TestObject>(
68+
() => testObjectBuilder.Value,
69+
o => ((TestObject.Builder)o).Build());
70+
71+
using (var db = SQLite3.OpenInMemory().AsAsyncDatabaseConnection())
72+
{
73+
var tableLookup =
74+
@"SELECT name FROM sqlite_master
75+
WHERE type='table' AND name='TestObject'
76+
ORDER BY name;";
77+
78+
// Table doesn't exist
79+
await db.DropTableIfExistsAsync(table);
80+
81+
await db.InitTableAsync(table);
82+
var count = await db.Query(tableLookup).Count();
83+
Assert.Greater(count, 0);
84+
85+
await db.DropTableIfExistsAsync(table);
86+
87+
count = await db.Query(tableLookup).Count();
88+
Assert.AreEqual(count, 0);
89+
}
90+
}
1791
}
1892
}
1993

SQLitePCL.pretty.tests/OrmTests/TableMappingTests.Sync.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,11 @@ public void TestDelete()
236236
Assert.Fail();
237237
}
238238

239+
if (db.TryDelete(table, 1000000, out deleted))
240+
{
241+
Assert.Fail();
242+
}
243+
239244
TestObject lookup;
240245
Assert.IsFalse(db.TryFind(table, inserted.Id.Value, out lookup));
241246
}
@@ -294,10 +299,13 @@ public void TestDropTable()
294299
WHERE type='table' AND name='TestObject'
295300
ORDER BY name;";
296301

302+
// Table doesn't exist
303+
db.DropTableIfExists(table);
304+
297305
db.InitTable(table);
298306
Assert.Greater(db.Query(tableLookup).Count(), 0);
299307

300-
db.DropTable(table);
308+
db.DropTableIfExists(table);
301309
Assert.AreEqual(db.Query(tableLookup).Count(), 0);
302310
}
303311
}

0 commit comments

Comments
 (0)