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 existTables = new List(); 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 GetTables() { return existTables; } } }