Skip to content

Commit 9c8c69c

Browse files
committed
NuGet v1.4.2, encapsulate table names
1 parent e741933 commit 9c8c69c

File tree

6 files changed

+29
-22
lines changed

6 files changed

+29
-22
lines changed

DatabaseWrapper/DatabaseWrapper.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;net452</TargetFrameworks>
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6-
<Version>1.4.1</Version>
6+
<Version>1.4.2</Version>
77
<Authors>Joel Christner</Authors>
88
<Company>Joel Christner</Company>
99
<Description>Simple database wrapper for Microsoft SQL Server, MySQL, and PostgreSQL written in C# supporting dynamic query building and nesting using expressions.</Description>
@@ -12,7 +12,7 @@
1212
<RepositoryUrl>https://github.com/jchristn/DatabaseWrapper</RepositoryUrl>
1313
<RepositoryType>Github</RepositoryType>
1414
<PackageLicenseUrl>https://github.com/jchristn/DatabaseWrapper/blob/master/LICENSE.txt</PackageLicenseUrl>
15-
<PackageReleaseNotes>Internal optimization and fixes</PackageReleaseNotes>
15+
<PackageReleaseNotes>Encapsulate table names in queries</PackageReleaseNotes>
1616
<PackageTags>database sql mysql mssql pgsql postgresql dynamic query builder datarow datatable</PackageTags>
1717
<PackageIconUrl>https://raw.githubusercontent.com/jchristn/databasewrapper/master/assets/icon.ico</PackageIconUrl>
1818
</PropertyGroup>

DatabaseWrapper/Helper/MssqlHelper.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ private static Column GetPrimaryKeyColumn(List<Column> columns)
180180
public static string CreateTableQuery(string tableName, List<Column> columns)
181181
{
182182
string query =
183-
"CREATE TABLE " + SanitizeString(tableName) +
183+
"CREATE TABLE [" + SanitizeString(tableName) + "] " +
184184
"(";
185185

186186
int added = 0;
@@ -220,7 +220,7 @@ public static string CreateTableQuery(string tableName, List<Column> columns)
220220

221221
public static string DropTableQuery(string tableName)
222222
{
223-
string query = "IF OBJECT_ID('dbo." + SanitizeString(tableName) + "', 'U') IS NOT NULL DROP TABLE dbo." + SanitizeString(tableName);
223+
string query = "IF OBJECT_ID('dbo." + SanitizeString(tableName) + "', 'U') IS NOT NULL DROP TABLE [" + SanitizeString(tableName) + "]";
224224
return query;
225225
}
226226

@@ -265,7 +265,7 @@ public static string SelectQuery(string tableName, int? indexStart, int? maxResu
265265
//
266266
// table
267267
//
268-
query += "FROM " + SanitizeString(tableName) + " ";
268+
query += "FROM [" + SanitizeString(tableName) + "] ";
269269

270270
//
271271
// expressions
@@ -303,7 +303,7 @@ public static string SelectQuery(string tableName, int? indexStart, int? maxResu
303303
public static string InsertQuery(string tableName, string keys, string values)
304304
{
305305
string ret =
306-
"INSERT INTO " + tableName + " WITH (ROWLOCK) " +
306+
"INSERT INTO [" + tableName + "] WITH (ROWLOCK) " +
307307
"(" + keys + ") " +
308308
"OUTPUT INSERTED.* " +
309309
"VALUES " +
@@ -315,7 +315,7 @@ public static string InsertQuery(string tableName, string keys, string values)
315315
public static string UpdateQuery(string tableName, string keyValueClause, Expression filter)
316316
{
317317
string ret =
318-
"UPDATE " + tableName + " WITH (ROWLOCK) SET " +
318+
"UPDATE [" + tableName + "] WITH (ROWLOCK) SET " +
319319
keyValueClause + " " +
320320
"OUTPUT INSERTED.* ";
321321

@@ -327,7 +327,7 @@ public static string UpdateQuery(string tableName, string keyValueClause, Expres
327327
public static string DeleteQuery(string tableName, Expression filter)
328328
{
329329
string ret =
330-
"DELETE FROM " + tableName + " WITH (ROWLOCK) ";
330+
"DELETE FROM [" + tableName + "] WITH (ROWLOCK) ";
331331

332332
if (filter != null) ret += "WHERE " + filter.ToWhereClause(DbTypes.MsSql) + " ";
333333

DatabaseWrapper/Helper/MysqlHelper.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public static string CreateTableQuery(string tableName, List<Column> columns)
121121

122122
public static string DropTableQuery(string tableName)
123123
{
124-
string query = "DROP TABLE IF EXISTS " + SanitizeString(tableName);
124+
string query = "DROP TABLE IF EXISTS `" + SanitizeString(tableName) + "`";
125125
return query;
126126
}
127127

@@ -161,7 +161,7 @@ public static string SelectQuery(string tableName, int? indexStart, int? maxResu
161161
//
162162
// table
163163
//
164-
outerQuery += "FROM " + tableName + " ";
164+
outerQuery += "FROM `" + tableName + "` ";
165165

166166
//
167167
// expressions
@@ -199,7 +199,7 @@ public static string InsertQuery(string tableName, string keys, string values)
199199
{
200200
string ret =
201201
"START TRANSACTION; " +
202-
"INSERT INTO " + tableName + " " +
202+
"INSERT INTO `" + tableName + "` " +
203203
"(" + keys + ") " +
204204
"VALUES " +
205205
"(" + values + "); " +
@@ -212,7 +212,7 @@ public static string InsertQuery(string tableName, string keys, string values)
212212
public static string UpdateQuery(string tableName, string keyValueClause, Expression filter)
213213
{
214214
string ret =
215-
"UPDATE " + tableName + " SET " +
215+
"UPDATE `" + tableName + "` SET " +
216216
keyValueClause + " ";
217217

218218
if (filter != null) ret += "WHERE " + filter.ToWhereClause(DbTypes.MySql) + " ";
@@ -223,7 +223,7 @@ public static string UpdateQuery(string tableName, string keyValueClause, Expres
223223
public static string DeleteQuery(string tableName, Expression filter)
224224
{
225225
string ret =
226-
"DELETE FROM " + tableName + " ";
226+
"DELETE FROM `" + tableName + "` ";
227227

228228
if (filter != null) ret += "WHERE " + filter.ToWhereClause(DbTypes.MySql) + " ";
229229

DatabaseWrapper/Helper/PgsqlHelper.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ private static Column GetPrimaryKeyColumn(List<Column> columns)
214214
public static string CreateTableQuery(string tableName, List<Column> columns)
215215
{
216216
string query =
217-
"CREATE TABLE " + SanitizeFieldname(tableName) + " " +
217+
"CREATE TABLE \"" + SanitizeFieldname(tableName) + "\" " +
218218
"(";
219219

220220
int added = 0;
@@ -237,7 +237,7 @@ public static string CreateTableQuery(string tableName, List<Column> columns)
237237

238238
public static string DropTableQuery(string tableName)
239239
{
240-
string query = "DROP TABLE IF EXISTS " + SanitizeFieldname(tableName);
240+
string query = "DROP TABLE IF EXISTS \"" + SanitizeFieldname(tableName) + "\"";
241241
return query;
242242
}
243243

@@ -277,7 +277,7 @@ public static string SelectQuery(string tableName, int? indexStart, int? maxResu
277277
//
278278
// table
279279
//
280-
outerQuery += "FROM " + tableName + " ";
280+
outerQuery += "FROM \"" + tableName + "\" ";
281281

282282
//
283283
// expressions
@@ -390,7 +390,7 @@ private static string PreparedOrderByClause(string val)
390390
public static string InsertQuery(string tableName, string keys, string values)
391391
{
392392
string ret =
393-
"INSERT INTO " + tableName + " " +
393+
"INSERT INTO \"" + tableName + "\" " +
394394
"(" + keys + ") " +
395395
"VALUES " +
396396
"(" + values + ") " +
@@ -401,7 +401,7 @@ public static string InsertQuery(string tableName, string keys, string values)
401401
public static string UpdateQuery(string tableName, string keyValueClause, Expression filter)
402402
{
403403
string ret =
404-
"UPDATE " + tableName + " SET " +
404+
"UPDATE \"" + tableName + "\" SET " +
405405
keyValueClause + " ";
406406

407407
if (filter != null) ret += "WHERE " + filter.ToWhereClause(DbTypes.PgSql) + " ";
@@ -413,7 +413,7 @@ public static string UpdateQuery(string tableName, string keyValueClause, Expres
413413
public static string DeleteQuery(string tableName, Expression filter)
414414
{
415415
string ret =
416-
"DELETE FROM " + tableName + " ";
416+
"DELETE FROM \"" + tableName + "\" ";
417417

418418
if (filter != null) ret += "WHERE " + filter.ToWhereClause(DbTypes.PgSql) + " ";
419419

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ Core features:
2424

2525
## New in v1.4.x
2626

27+
- Encapsulate table names in queries with the appropriate characters
28+
- Microsoft SQL: ```[tablename]```
29+
- MySQL: ````[tablename]````
30+
- PostgreSQL: ```"tablename"```
2731
- Add support for CreateTable and DropTable operations, please note the following constraints:
2832
- For PostgreSQL, automatically uses ```SERIAL PRIMARY KEY``` for primary keys
2933
- For Microsoft SQL, automatically creates a constraint and assumes primary key type is ```int```

Test/Program.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,19 @@ static void Main(string[] args)
3232
string dbType = Console.ReadLine();
3333
if (String.IsNullOrEmpty(dbType)) return;
3434

35+
Console.Write("Password: ");
36+
string password = Console.ReadLine();
37+
3538
switch (dbType)
3639
{
3740
case "mssql":
38-
client = new DatabaseClient(DbTypes.MsSql, "localhost", 1433, "sa", "<password>", null, "test");
41+
client = new DatabaseClient(DbTypes.MsSql, "localhost", 1433, "sa", password, null, "test");
3942
break;
4043
case "mysql":
41-
client = new DatabaseClient(DbTypes.MySql, "localhost", 3306, "root", "<password>", null, "test");
44+
client = new DatabaseClient(DbTypes.MySql, "localhost", 3306, "root", password, null, "test");
4245
break;
4346
case "pgsql":
44-
client = new DatabaseClient(DbTypes.PgSql, "localhost", 5432, "postgres", "<password>", null, "test");
47+
client = new DatabaseClient(DbTypes.PgSql, "localhost", 5432, "postgres", password, null, "test");
4548
break;
4649
default:
4750
return;

0 commit comments

Comments
 (0)