Skip to content

Commit f237e52

Browse files
committed
Improving code completion
1 parent f8c529c commit f237e52

37 files changed

+1438
-1022
lines changed

DataCommander.Foundation/Data/DataColumnSchema.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ public DataColumnSchema(DataRow schemaTableRow)
2323
this.schemaTableRow = schemaTableRow;
2424
}
2525

26+
/// <summary>
27+
///
28+
/// </summary>
29+
/// <param name="name"></param>
30+
/// <returns></returns>
31+
public object this[string name]
32+
{
33+
get
34+
{
35+
return this.schemaTableRow[name];
36+
}
37+
}
38+
2639
/// <summary>
2740
///
2841
/// </summary>

DataCommander.Foundation/Data/DataTableExtensions.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,38 @@ public static StringTable ToStringTable(this DataTable dataTable)
2020
{
2121
Contract.Requires(dataTable != null);
2222

23-
DataColumnCollection dataColumns = dataTable.Columns;
23+
var dataColumns = dataTable.Columns;
2424
Int32 columnCount = dataColumns.Count;
25-
StringTable st = new StringTable(columnCount);
26-
SetAlign(dataColumns, st.Columns);
27-
WriteHeader(dataTable.Columns, st);
25+
var stringTable = new StringTable(columnCount);
26+
SetAlign(dataColumns, stringTable.Columns);
27+
WriteHeader(dataTable.Columns, stringTable);
2828

2929
foreach (DataRow dataRow in dataTable.Rows)
3030
{
3131
DataRowState rowState = dataRow.RowState;
3232

3333
if (rowState != DataRowState.Deleted)
3434
{
35-
StringTableRow row = st.NewRow();
35+
StringTableRow stringTableRow = stringTable.NewRow();
3636
Object[] itemArray = dataRow.ItemArray;
3737

3838
for (Int32 i = 0; i < itemArray.Length; i++)
3939
{
40-
row[i] = itemArray[i].ToString();
40+
object value = itemArray[i];
41+
42+
string valueString = value == DBNull.Value
43+
? Database.NullString
44+
: value.ToString();
45+
46+
stringTableRow[i] = valueString;
4147
}
4248

43-
st.Rows.Add(row);
49+
stringTable.Rows.Add(stringTableRow);
4450
}
4551
}
4652

47-
WriteHeaderSeparator(st);
48-
return st;
53+
WriteHeaderSeparator(stringTable);
54+
return stringTable;
4955
}
5056

5157
internal static void SetAlign(IEnumerable dataColumns, StringTableColumnCollection columns)

DataCommander.Foundation/Linq/IListExtensions.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,46 @@ public static void Remove(this IList list, IEnumerable items)
4646
}
4747
}
4848

49+
/// <summary>
50+
///
51+
/// </summary>
52+
/// <typeparam name="T"></typeparam>
53+
/// <param name="list"></param>
54+
/// <param name="compareTo"></param>
55+
/// <returns></returns>
56+
public static IndexedItem<T> BinarySearch<T>(this IList<T> list, Func<T, int> compareTo)
57+
{
58+
Contract.Requires(list != null);
59+
Contract.Requires(compareTo != null);
60+
61+
IndexedItem<T> result = null;
62+
63+
Int32 from = 0;
64+
Int32 to = list.Count - 1;
65+
66+
while (from <= to)
67+
{
68+
Int32 index = from + (to - from)/2;
69+
var value = list[index];
70+
Int32 comparisonResult = compareTo(value);
71+
if (comparisonResult == 0)
72+
{
73+
result = new IndexedItem<T>(index, value);
74+
break;
75+
}
76+
else if (comparisonResult < 0)
77+
{
78+
to = index - 1;
79+
}
80+
else
81+
{
82+
from = index + 1;
83+
}
84+
}
85+
86+
return result;
87+
}
88+
4989
/// <summary>
5090
///
5191
/// </summary>

DataCommander.Providers.SqlServer2005/DataCommander.Providers.SqlServer2005.csproj

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<BootstrapperEnabled>true</BootstrapperEnabled>
3434
<TargetFrameworkProfile>
3535
</TargetFrameworkProfile>
36-
<CodeContractsAssemblyMode>0</CodeContractsAssemblyMode>
36+
<CodeContractsAssemblyMode>1</CodeContractsAssemblyMode>
3737
</PropertyGroup>
3838
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
3939
<DebugSymbols>true</DebugSymbols>
@@ -44,6 +44,48 @@
4444
<ErrorReport>prompt</ErrorReport>
4545
<WarningLevel>4</WarningLevel>
4646
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
47+
<CodeContractsEnableRuntimeChecking>True</CodeContractsEnableRuntimeChecking>
48+
<CodeContractsRuntimeOnlyPublicSurface>False</CodeContractsRuntimeOnlyPublicSurface>
49+
<CodeContractsRuntimeThrowOnFailure>True</CodeContractsRuntimeThrowOnFailure>
50+
<CodeContractsRuntimeCallSiteRequires>False</CodeContractsRuntimeCallSiteRequires>
51+
<CodeContractsRuntimeSkipQuantifiers>False</CodeContractsRuntimeSkipQuantifiers>
52+
<CodeContractsRunCodeAnalysis>False</CodeContractsRunCodeAnalysis>
53+
<CodeContractsNonNullObligations>True</CodeContractsNonNullObligations>
54+
<CodeContractsBoundsObligations>True</CodeContractsBoundsObligations>
55+
<CodeContractsArithmeticObligations>True</CodeContractsArithmeticObligations>
56+
<CodeContractsEnumObligations>True</CodeContractsEnumObligations>
57+
<CodeContractsRedundantAssumptions>True</CodeContractsRedundantAssumptions>
58+
<CodeContractsAssertsToContractsCheckBox>True</CodeContractsAssertsToContractsCheckBox>
59+
<CodeContractsRedundantTests>True</CodeContractsRedundantTests>
60+
<CodeContractsMissingPublicRequiresAsWarnings>True</CodeContractsMissingPublicRequiresAsWarnings>
61+
<CodeContractsMissingPublicEnsuresAsWarnings>False</CodeContractsMissingPublicEnsuresAsWarnings>
62+
<CodeContractsInferRequires>True</CodeContractsInferRequires>
63+
<CodeContractsInferEnsures>False</CodeContractsInferEnsures>
64+
<CodeContractsInferObjectInvariants>False</CodeContractsInferObjectInvariants>
65+
<CodeContractsSuggestAssumptions>False</CodeContractsSuggestAssumptions>
66+
<CodeContractsSuggestAssumptionsForCallees>False</CodeContractsSuggestAssumptionsForCallees>
67+
<CodeContractsSuggestRequires>False</CodeContractsSuggestRequires>
68+
<CodeContractsNecessaryEnsures>True</CodeContractsNecessaryEnsures>
69+
<CodeContractsSuggestObjectInvariants>False</CodeContractsSuggestObjectInvariants>
70+
<CodeContractsSuggestReadonly>True</CodeContractsSuggestReadonly>
71+
<CodeContractsRunInBackground>True</CodeContractsRunInBackground>
72+
<CodeContractsShowSquigglies>True</CodeContractsShowSquigglies>
73+
<CodeContractsUseBaseLine>False</CodeContractsUseBaseLine>
74+
<CodeContractsEmitXMLDocs>False</CodeContractsEmitXMLDocs>
75+
<CodeContractsCustomRewriterAssembly />
76+
<CodeContractsCustomRewriterClass />
77+
<CodeContractsLibPaths />
78+
<CodeContractsExtraRewriteOptions />
79+
<CodeContractsExtraAnalysisOptions />
80+
<CodeContractsSQLServerOption />
81+
<CodeContractsBaseLineFile />
82+
<CodeContractsCacheAnalysisResults>True</CodeContractsCacheAnalysisResults>
83+
<CodeContractsSkipAnalysisIfCannotConnectToCache>False</CodeContractsSkipAnalysisIfCannotConnectToCache>
84+
<CodeContractsFailBuildOnWarnings>False</CodeContractsFailBuildOnWarnings>
85+
<CodeContractsBeingOptimisticOnExternal>True</CodeContractsBeingOptimisticOnExternal>
86+
<CodeContractsRuntimeCheckingLevel>Full</CodeContractsRuntimeCheckingLevel>
87+
<CodeContractsReferenceAssembly>%28none%29</CodeContractsReferenceAssembly>
88+
<CodeContractsAnalysisWarningLevel>0</CodeContractsAnalysisWarningLevel>
4789
</PropertyGroup>
4890
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
4991
<DebugType>pdbonly</DebugType>
@@ -143,6 +185,9 @@
143185
<Compile Include="ObjectExplorer\UserDefinedTableTypeNode.cs" />
144186
<Compile Include="ObjectName.cs" />
145187
<Compile Include="SqlDataReaderHelper.cs" />
188+
<Compile Include="SqlObjectTypesExtensions.cs" />
189+
<Compile Include="SqlServerObject.cs" />
190+
<Compile Include="SqlServerObjectType.cs" />
146191
<Compile Include="SqlServerProvider.cs" />
147192
<Compile Include="ObjectExplorer\StoredProcedureCollectionNode.cs" />
148193
<Compile Include="ObjectExplorer\StoredProcedureNode.cs" />

DataCommander.Providers.SqlServer2005/ObjectExplorer/TableNode.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ public bool Sortable
6969

7070
internal static string GetSelectStatement(
7171
IDbConnection connection,
72-
FourPartName fourPartName )
72+
DatabaseObjectMultipartName databaseObjectMultipartName )
7373
{
7474
Contract.Requires( connection != null );
75-
Contract.Requires( fourPartName != null );
75+
Contract.Requires( databaseObjectMultipartName != null );
7676

7777
string commandText = string.Format(@"select c.name
7878
from [{0}].sys.schemas s (nolock)
@@ -84,9 +84,9 @@ internal static string GetSelectStatement(
8484
s.name = '{1}'
8585
and t.name = '{2}'
8686
order by c.column_id",
87-
fourPartName.Database,
88-
fourPartName.Owner,
89-
fourPartName.Name);
87+
databaseObjectMultipartName.Database,
88+
databaseObjectMultipartName.Schema,
89+
databaseObjectMultipartName.Name);
9090

9191
var columnNames = new StringBuilder();
9292
bool first = true;
@@ -117,7 +117,7 @@ internal static string GetSelectStatement(
117117

118118
string query = string.Format(
119119
@"select {0}
120-
from [{1}].[{2}].[{3}]", columnNames, fourPartName.Database, fourPartName.Owner, fourPartName.Name );
120+
from [{1}].[{2}].[{3}]", columnNames, databaseObjectMultipartName.Database, databaseObjectMultipartName.Schema, databaseObjectMultipartName.Name );
121121

122122
return query;
123123
}
@@ -126,7 +126,7 @@ public string Query
126126
{
127127
get
128128
{
129-
var name = new FourPartName( null, this.database.Name, this.owner, this.name );
129+
var name = new DatabaseObjectMultipartName( null, this.database.Name, this.owner, this.name );
130130
string connectionString = this.database.Databases.Server.ConnectionString;
131131
string text;
132132
using (var connection = new SqlConnection( connectionString ))
@@ -387,7 +387,7 @@ private void Indexes_Click( object sender, EventArgs e )
387387

388388
private void SelectScript_Click( object sender, EventArgs e )
389389
{
390-
FourPartName name = new FourPartName( null, this.database.Name, this.owner, this.name );
390+
DatabaseObjectMultipartName name = new DatabaseObjectMultipartName( null, this.database.Name, this.owner, this.name );
391391
string connectionString = this.database.Databases.Server.ConnectionString;
392392
string selectStatement;
393393
using (var connection = new SqlConnection( connectionString ))

DataCommander.Providers.SqlServer2005/ObjectExplorer/TriggerCollectionNode.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public bool IsLeaf
3939
IEnumerable<ITreeNode> ITreeNode.GetChildren( bool refresh )
4040
{
4141
var cb = new SqlCommandBuilder();
42-
var tableName = new FourPartName( null, this.database.Name, this.schema, this.objectName );
42+
var tableName = new DatabaseObjectMultipartName( null, this.database.Name, this.schema, this.objectName );
4343
//string commandText = "select o1.name,o2.name from {0}..sysobjects o1 left join {0}..sysobjects o2 on o1.parent_obj = o2.id where o1.type = 'TR' and o2.name = '{1}'";
4444
string commandText = string.Format( @"select tr.name
4545
from {0}.sys.schemas s
@@ -51,7 +51,7 @@ IEnumerable<ITreeNode> ITreeNode.GetChildren( bool refresh )
5151
and o.name = {2}
5252
order by 1",
5353
cb.QuoteIdentifier( this.database.Name ),
54-
tableName.Owner.ToTSqlNVarChar(),
54+
tableName.Schema.ToTSqlNVarChar(),
5555
tableName.Name.ToTSqlNVarChar() );
5656

5757
string connectionString = this.database.Databases.Server.ConnectionString;

DataCommander.Providers.SqlServer2005/ObjectExplorer/ViewNode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public string Query
5959
{
6060
get
6161
{
62-
FourPartName name = new FourPartName(null, this.database.Name, this.schema, this.name);
62+
DatabaseObjectMultipartName name = new DatabaseObjectMultipartName(null, this.database.Name, this.schema, this.name);
6363
string connectionString = this.database.Databases.Server.ConnectionString;
6464
string text;
6565
using (var connection = new SqlConnection(connectionString))
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace DataCommander.Providers.SqlServer2005
2+
{
3+
using System.Collections.Generic;
4+
5+
internal static class SqlObjectTypesExtensions
6+
{
7+
public static List<string> ToObjectTypes(this SqlObjectTypes sqlObjectTypes)
8+
{
9+
var list = new List<string>();
10+
11+
if (sqlObjectTypes.HasFlag(SqlObjectTypes.Table))
12+
{
13+
list.Add(SqlServerObjectType.UserDefinedTable);
14+
list.Add(SqlServerObjectType.SystemTable);
15+
}
16+
17+
if (sqlObjectTypes.HasFlag(SqlObjectTypes.View))
18+
{
19+
list.Add(SqlServerObjectType.View);
20+
}
21+
22+
if (sqlObjectTypes.HasFlag(SqlObjectTypes.Function))
23+
{
24+
list.Add(SqlServerObjectType.ScalarFunction);
25+
list.Add(SqlServerObjectType.InlineTableValuedFunction);
26+
list.Add(SqlServerObjectType.TableValuedFunction);
27+
}
28+
29+
return list;
30+
}
31+
}
32+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
namespace DataCommander.Providers.SqlServer2005
2+
{
3+
using System.Collections.Generic;
4+
using System.Diagnostics.Contracts;
5+
using System.Linq;
6+
using DataCommander.Foundation.Data.SqlClient;
7+
8+
internal static class SqlServerObject
9+
{
10+
public static string GetDatabases()
11+
{
12+
return "select name from sys.databases (nolock) order by name";
13+
}
14+
15+
public static string GetSchemas()
16+
{
17+
return "select name from sys.schemas (nolock) order by name";
18+
}
19+
20+
public static string GetSchemas(string database)
21+
{
22+
Contract.Requires(!string.IsNullOrWhiteSpace(database));
23+
24+
return string.Format(@"if exists(select * from sys.databases (nolock) where name = '{0}')
25+
begin
26+
select null,name
27+
from [{0}].sys.schemas (nolock)
28+
order by name
29+
end", database);
30+
}
31+
32+
public static string GetObjects(string schema, IEnumerable<string> objectTypes)
33+
{
34+
Contract.Requires(!string.IsNullOrWhiteSpace(schema));
35+
Contract.Requires(objectTypes != null && objectTypes.Any());
36+
37+
return string.Format(@"declare @schema_id int
38+
39+
select @schema_id = schema_id
40+
from sys.schemas (nolock)
41+
where name = '{0}'
42+
43+
if @schema_id is not null
44+
begin
45+
select o.name
46+
from sys.all_objects o (nolock)
47+
where
48+
o.schema_id = @schema_id
49+
and o.type in({1})
50+
order by o.name
51+
end", schema, string.Join(",", objectTypes.Select(o=>o.ToTSqlVarChar())));
52+
}
53+
54+
public static string GetObjects(
55+
string database,
56+
string schema,
57+
IEnumerable<string> objectTypes)
58+
{
59+
Contract.Requires(!string.IsNullOrWhiteSpace(database));
60+
Contract.Requires(!string.IsNullOrWhiteSpace(schema));
61+
Contract.Requires(objectTypes!=null && objectTypes.Any());
62+
63+
return string.Format(@"if exists(select * from sys.databases (nolock) where name = '{0}')
64+
begin
65+
declare @schema_id int
66+
67+
select @schema_id = schema_id
68+
from [{0}].sys.schemas (nolock)
69+
where name = '{1}'
70+
71+
if @schema_id is not null
72+
begin
73+
select o.name
74+
from [{0}].sys.all_objects o (nolock)
75+
where
76+
o.schema_id = @schema_id
77+
and o.type in({2})
78+
order by o.name
79+
end
80+
end", database, schema, string.Join(",", objectTypes.Select(t => t.ToTSqlVarChar())));
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)