68 lines
2.4 KiB
C#
68 lines
2.4 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Data.SqlClient;
|
||
|
||
using Zhaizj.Framework.ORM;
|
||
|
||
namespace Zhaizj.Framework.Data {
|
||
|
||
|
||
internal class SQLServerDatabaseChecker : IDatabaseChecker {
|
||
|
||
private static readonly ILog logger = LogManager.GetLogger( typeof( SQLServerDatabaseChecker ) );
|
||
|
||
private String _connectionString;
|
||
private DatabaseType _databaseType;
|
||
|
||
public String ConnectionString {
|
||
get { return _connectionString; }
|
||
set { _connectionString = value; }
|
||
}
|
||
|
||
public DatabaseType DatabaseType {
|
||
get { return _databaseType; }
|
||
set { _databaseType = value; }
|
||
}
|
||
|
||
private List<String> existTables = new List<String>();
|
||
|
||
public void CheckDatabase() {
|
||
if (strUtil.IsNullOrEmpty( _connectionString )) {
|
||
throw new Exception( "[sqlserver] connection String is not found" );
|
||
}
|
||
IDatabaseDialect dialect = DataFactory.GetDialect( DatabaseType.SqlServer );
|
||
if (strUtil.IsNullOrEmpty( dialect.GetConnectionItem( _connectionString, ConnectionItemType.Server ) )) {
|
||
throw new Exception( "[sqlserver] address is empty" );
|
||
}
|
||
if (strUtil.IsNullOrEmpty( dialect.GetConnectionItem( _connectionString, ConnectionItemType.Database ) )) {
|
||
throw new Exception( "[sqlserver] database is empty" );
|
||
}
|
||
}
|
||
|
||
public void CheckTable( MappingClass mapping, String db ) {
|
||
logger.Info( "[sqlserver] begin check table" );
|
||
SqlConnection connection = new SqlConnection( _connectionString );
|
||
connection.Open();
|
||
SqlCommand cmd = new SqlCommand();
|
||
cmd.Connection = connection;
|
||
cmd.CommandText = "SELECT OBJECT_NAME(id) as name FROM sysobjects WHERE xtype='U' AND OBJECTPROPERTY(id, 'IsMSShipped') = 0";
|
||
SqlDataReader reader = cmd.ExecuteReader();
|
||
while (reader.Read()) {
|
||
existTables.Add( reader["name"].ToString() );
|
||
logger.Info( "table found:" + reader["name"].ToString() );
|
||
}
|
||
reader.Close();
|
||
existTables = new SqlServerTableBuilder().CheckMappingTableIsExist( cmd, db, existTables, mapping );
|
||
|
||
connection.Close();
|
||
}
|
||
|
||
public List<String> GetTables() {
|
||
return existTables;
|
||
}
|
||
|
||
}
|
||
}
|
||
|