Skip to content

Commit

Permalink
Fix: Support for non-dbo schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanJard committed Jul 17, 2020
1 parent fe2b227 commit 131d548
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions DataTables-Editor-Server/DataBaseUtil/Sqlserver/Query.cs
Expand Up @@ -73,6 +73,12 @@ override protected void _Prepare(string sql)
if (_type == "insert")
{
var pkeyCmd = provider.CreateCommand();
var parts = _table[0].Split('.');
var schemaName = parts.Count() > 1 ? parts[0] : "";
var tableName = parts.Count() > 1 ? parts[1] : _table[0];
var schemaQuery = schemaName != "" ?
" KCU.TABLE_SCHEMA = @schema AND " :
"";

// We need to find out what the primary key column name and type is
pkeyCmd.CommandText = @"
Expand All @@ -84,8 +90,9 @@ override protected void _Prepare(string sql)
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU ON
TC.CONSTRAINT_TYPE = 'PRIMARY KEY' AND
TC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME AND
KCU.TABLE_NAME = @table
TC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME AND " +
schemaQuery +
@"KCU.TABLE_NAME = @table
JOIN
INFORMATION_SCHEMA.COLUMNS as C ON
C.table_name = KCU.table_name AND
Expand All @@ -97,9 +104,16 @@ override protected void _Prepare(string sql)

param = pkeyCmd.CreateParameter();
param.ParameterName = "@table";
param.Value = _table[0];
param.Value = tableName;
pkeyCmd.Parameters.Add(param);

if (schemaName != "") {
param = pkeyCmd.CreateParameter();
param.ParameterName = "@schema";
param.Value = schemaName;
pkeyCmd.Parameters.Add(param);
}

using (var dr = pkeyCmd.ExecuteReader())
{
// If the table doesn't have a primary key field, we can't get
Expand Down

0 comments on commit 131d548

Please sign in to comment.