Skip to content

Commit

Permalink
#1015 - Fixes for the dotted schema.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikependon committed Jan 14, 2022
1 parent 595ade6 commit cabdf5d
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 32 deletions.
43 changes: 31 additions & 12 deletions RepoDb.Core/RepoDb/Extensions/DataEntityExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public static string GetMappedName<TEntity>() =>
/// </summary>
/// <param name="tableName">The name of the table.</param>
/// <returns>The schema of the passed table name.</returns>
[Obsolete("Use the overloaded method instead.")]
public static string GetSchema(string tableName) =>
GetSchema(tableName, null);

Expand All @@ -90,14 +91,24 @@ public static string GetSchema(string tableName) =>
public static string GetSchema(string tableName,
IDbSetting dbSetting)
{
// Get the schema and table name
var index = tableName.IndexOf(CharConstant.Period);
if (index > 0)
if (tableName.IsOpenQuoted(dbSetting))
{
return tableName.Substring(0, index).AsUnquoted(true, dbSetting);
var index = tableName.IndexOf(string.Concat(dbSetting.ClosingQuote, CharConstant.Period));
if (index >= 0)
{
return tableName.Substring(0, index + 1);
}
}
else
{
var index = tableName.IndexOf(CharConstant.Period);
if (index >= 0)
{
return tableName.Substring(0, index);
}

}

// Return the unquoted
return dbSetting?.DefaultSchema;
}

Expand All @@ -106,6 +117,7 @@ public static string GetSchema(string tableName,
/// </summary>
/// <param name="tableName">The name of the table.</param>
/// <returns>The actual table name.</returns>
[Obsolete("Use the overloaded method instead.")]
public static string GetTableName(string tableName) =>
GetTableName(tableName, null);

Expand All @@ -118,18 +130,25 @@ public static string GetTableName(string tableName) =>
public static string GetTableName(string tableName,
IDbSetting dbSetting)
{
// Get the schema and table name
var index = tableName.IndexOf(CharConstant.Period);
if (index > 0)
if (tableName.IsOpenQuoted(dbSetting))
{
var index = tableName.IndexOf(string.Concat(dbSetting.ClosingQuote, CharConstant.Period));
if (index >= 0 && tableName.Length > index + 2)
{
return tableName.Substring(index + 2);
}
}
else
{
if (tableName.Length > index)
var index = tableName.IndexOf(CharConstant.Period);
if (index >= 0 && tableName.Length > index)
{
return tableName.Substring(index + 1).AsUnquoted(true, dbSetting);
return tableName.Substring(index + 1);
}

}

// Return the unquoted
return tableName.AsUnquoted(true, dbSetting);
return tableName;
}

/// <summary>
Expand Down
20 changes: 20 additions & 0 deletions RepoDb.Core/RepoDb/Extensions/StringExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ public static string AsAlphaNumeric(this string value,
return alphaNumericRegex.Replace(value, "_");
}

/// <summary>
/// Check whether the string value is open-quoted.
/// </summary>
/// <param name="value">The string value to be checked.</param>
/// <param name="dbSetting">The currently in used <see cref="IDbSetting"/> object.</param>
/// <returns>True if the value is open-quoted.</returns>
public static bool IsOpenQuoted(this string value,
IDbSetting dbSetting) =>
dbSetting != null ? value.StartsWith(dbSetting.OpeningQuote) : false;

/// <summary>
/// Check whether the string value is close-quoted.
/// </summary>
/// <param name="value">The string value to be checked.</param>
/// <param name="dbSetting">The currently in used <see cref="IDbSetting"/> object.</param>
/// <returns>True if the value is close-quoted.</returns>
public static bool IsCloseQuoted(this string value,
IDbSetting dbSetting) =>
dbSetting != null ? value.EndsWith(dbSetting.ClosingQuote) : false;

/// <summary>
/// Unquotes a string.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions RepoDb.MySql/RepoDb.MySql/DbHelpers/MySqlDbHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public IEnumerable<DbField> GetFields(IDbConnection connection,
var param = new
{
TableSchema = connection.Database,
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting)
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting).AsUnquoted(m_dbSetting)
};

// Iterate and extract
Expand Down Expand Up @@ -209,7 +209,7 @@ public async Task<IEnumerable<DbField>> GetFieldsAsync(IDbConnection connection,
var param = new
{
TableSchema = connection.Database,
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting)
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting).AsUnquoted(m_dbSetting)
};

// Iterate and extract
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public IEnumerable<DbField> GetFields(IDbConnection connection,
var param = new
{
TableSchema = connection.Database,
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting)
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting).AsUnquoted(m_dbSetting)
};

// Iterate and extract
Expand Down Expand Up @@ -209,7 +209,7 @@ public async Task<IEnumerable<DbField>> GetFieldsAsync(IDbConnection connection,
var param = new
{
TableSchema = connection.Database,
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting)
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting).AsUnquoted(m_dbSetting)
};

// Iterate and extract
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ public IEnumerable<DbField> GetFields(IDbConnection connection,
var commandText = GetCommandText();
var param = new
{
Schema = DataEntityExtension.GetSchema(tableName, m_dbSetting),
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting)
Schema = DataEntityExtension.GetSchema(tableName, m_dbSetting).AsUnquoted(m_dbSetting),
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting).AsUnquoted(m_dbSetting)
};

// Iterate and extract
Expand Down Expand Up @@ -183,8 +183,8 @@ public async Task<IEnumerable<DbField>> GetFieldsAsync(IDbConnection connection,
var commandText = GetCommandText();
var param = new
{
Schema = DataEntityExtension.GetSchema(tableName, m_dbSetting),
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting)
Schema = DataEntityExtension.GetSchema(tableName, m_dbSetting).AsUnquoted(m_dbSetting),
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting).AsUnquoted(m_dbSetting)
};

// Iterate and extract
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public SqLiteDbHelper(IDbSetting dbSetting,
/// <param name="tableName"></param>
/// <returns></returns>
private string GetCommandText(string tableName) =>
$"pragma table_info({DataEntityExtension.GetTableName(tableName)});";
$"pragma table_info({DataEntityExtension.GetTableName(tableName, DbSetting).AsUnquoted(DbSetting)});";

/// <summary>
///
Expand Down Expand Up @@ -113,7 +113,7 @@ private string GetIdentityFieldName<TDbConnection>(TDbConnection connection,
// Sql text
var commandText = "SELECT sql FROM [sqlite_master] WHERE name = @TableName AND type = 'table';";
var sql = connection.ExecuteScalar<string>(commandText: commandText,
param: new { TableName = DataEntityExtension.GetTableName(tableName).AsUnquoted(DbSetting) },
param: new { TableName = DataEntityExtension.GetTableName(tableName, DbSetting).AsUnquoted(DbSetting) },
transaction: transaction);

// Return
Expand All @@ -140,7 +140,7 @@ private async Task<string> GetIdentityFieldNameAsync<TDbConnection>(TDbConnectio
// Sql text
var commandText = "SELECT sql FROM [sqlite_master] WHERE name = @TableName AND type = 'table';";
var sql = await connection.ExecuteScalarAsync<string>(commandText: commandText,
param: new { TableName = DataEntityExtension.GetTableName(tableName).AsUnquoted(DbSetting) },
param: new { TableName = DataEntityExtension.GetTableName(tableName, DbSetting).AsUnquoted(DbSetting) },
transaction: transaction,
cancellationToken: cancellationToken);

Expand Down Expand Up @@ -197,7 +197,7 @@ private string GetIdentityFieldNameInternal(string sql)
private bool IsIdentity(string field)
{
return field.Contains("AUTOINCREMENT", StringComparison.OrdinalIgnoreCase) ||
(field.Contains("INTEGER", StringComparison.OrdinalIgnoreCase)
(field.Contains("INTEGER", StringComparison.OrdinalIgnoreCase)
&& field.Contains("PRIMARY KEY", StringComparison.OrdinalIgnoreCase));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ public IEnumerable<DbField> GetFields(IDbConnection connection,
var setting = connection.GetDbSetting();
var param = new
{
Schema = DataEntityExtension.GetSchema(tableName, setting),
TableName = DataEntityExtension.GetTableName(tableName, setting)
Schema = DataEntityExtension.GetSchema(tableName, setting).AsUnquoted(setting),
TableName = DataEntityExtension.GetTableName(tableName, setting).AsUnquoted(setting)
};

// Iterate and extract
Expand Down Expand Up @@ -194,8 +194,8 @@ public async Task<IEnumerable<DbField>> GetFieldsAsync(IDbConnection connection,
var setting = connection.GetDbSetting();
var param = new
{
Schema = DataEntityExtension.GetSchema(tableName, setting),
TableName = DataEntityExtension.GetTableName(tableName, setting)
Schema = DataEntityExtension.GetSchema(tableName, setting).AsUnquoted(setting),
TableName = DataEntityExtension.GetTableName(tableName, setting).AsUnquoted(setting)
};

// Iterate and extract
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public SqLiteDbHelper(IDbSetting dbSetting,
/// <param name="tableName"></param>
/// <returns></returns>
private string GetCommandText(string tableName) =>
$"pragma table_info({DataEntityExtension.GetTableName(tableName)});";
$"pragma table_info({DataEntityExtension.GetTableName(tableName, DbSetting).AsUnquoted(DbSetting)});";

/// <summary>
///
Expand Down Expand Up @@ -113,7 +113,7 @@ private string GetIdentityFieldName<TDbConnection>(TDbConnection connection,
// Sql text
var commandText = "SELECT sql FROM [sqlite_master] WHERE name = @TableName AND type = 'table';";
var sql = connection.ExecuteScalar<string>(commandText: commandText,
param: new { TableName = DataEntityExtension.GetTableName(tableName).AsUnquoted(DbSetting) },
param: new { TableName = DataEntityExtension.GetTableName(tableName, DbSetting).AsUnquoted(DbSetting) },
transaction: transaction);

// Return
Expand All @@ -140,7 +140,7 @@ private async Task<string> GetIdentityFieldNameAsync<TDbConnection>(TDbConnectio
// Sql text
var commandText = "SELECT sql FROM [sqlite_master] WHERE name = @TableName AND type = 'table';";
var sql = await connection.ExecuteScalarAsync<string>(commandText: commandText,
param: new { TableName = DataEntityExtension.GetTableName(tableName).AsUnquoted(DbSetting) },
param: new { TableName = DataEntityExtension.GetTableName(tableName, DbSetting).AsUnquoted(DbSetting) },
transaction: transaction,
cancellationToken: cancellationToken);

Expand Down Expand Up @@ -197,7 +197,7 @@ private string GetIdentityFieldNameInternal(string sql)
private bool IsIdentity(string field)
{
return field.Contains("AUTOINCREMENT", StringComparison.OrdinalIgnoreCase) ||
(field.Contains("INTEGER", StringComparison.OrdinalIgnoreCase)
(field.Contains("INTEGER", StringComparison.OrdinalIgnoreCase)
&& field.Contains("PRIMARY KEY", StringComparison.OrdinalIgnoreCase));
}

Expand Down

0 comments on commit cabdf5d

Please sign in to comment.