init lserp cs 5.0
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.OleDb;
|
||||
using System.IO;
|
||||
|
||||
using Zhaizj.Framework.ORM;
|
||||
|
||||
namespace Zhaizj.Framework.Data {
|
||||
|
||||
internal class AccessDatabaseChecker : IDatabaseChecker {
|
||||
|
||||
private static readonly ILog logger = LogManager.GetLogger( typeof( AccessDatabaseChecker ) );
|
||||
|
||||
private String _connectionString;
|
||||
private List<String> existTables = new List<String>();
|
||||
|
||||
public String ConnectionString {
|
||||
get { return _connectionString; }
|
||||
set { _connectionString = value; }
|
||||
}
|
||||
|
||||
public DatabaseType DatabaseType {
|
||||
get { return DatabaseType.Access; }
|
||||
set { }
|
||||
}
|
||||
|
||||
public void CheckDatabase() {
|
||||
logger.Info( "begin check database" );
|
||||
if (strUtil.IsNullOrEmpty( _connectionString )) {
|
||||
logger.Info( "connection String is empty. begin to create access database and set connection string" );
|
||||
createDatabaseAndSaveConnectionString();
|
||||
}
|
||||
else {
|
||||
String connectionItem = DataFactory.GetDialect( DatabaseType.Access ).GetConnectionItem( _connectionString, ConnectionItemType.Database );
|
||||
if (strUtil.IsNullOrEmpty( connectionItem )) {
|
||||
logger.Info( "connection String is found, but database is empty. begin to create access database and set connection string" );
|
||||
createDatabaseAndSaveConnectionString();
|
||||
}
|
||||
else if (!File.Exists( connectionItem )) {
|
||||
logger.Info( "ConnectionString:" + _connectionString );
|
||||
logger.Info( "the database [" + connectionItem + "] is not found. begin to create access database and set connection string" );
|
||||
DatabaseBuilder.BuildAccessDb4o( connectionItem );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void CheckTable( MappingClass mapping, String db ) {
|
||||
logger.Info( "[access] begin check table" );
|
||||
|
||||
OleDbConnection connection = DataFactory.GetConnection( _connectionString, this.DatabaseType ) as OleDbConnection;
|
||||
connection.Open();
|
||||
IDbCommand cmd = new OleDbCommand();
|
||||
cmd.Connection = connection;
|
||||
object[] restrictions = new object[4];
|
||||
restrictions[3] = "TABLE";
|
||||
DataTable oleDbSchemaTable = connection.GetOleDbSchemaTable( OleDbSchemaGuid.Tables, restrictions );
|
||||
foreach (DataRow row in oleDbSchemaTable.Rows) {
|
||||
existTables.Add( row["TABLE_NAME"].ToString() );
|
||||
logger.Info( "table found£º" + row["TABLE_NAME"].ToString() );
|
||||
}
|
||||
existTables = new AccessTableBuilder().CheckMappingTableIsExist( cmd, db, existTables, mapping );
|
||||
connection.Close();
|
||||
}
|
||||
|
||||
private void createDatabaseAndSaveConnectionString() {
|
||||
_connectionString = DatabaseBuilder.ConnectionStringPrefix + DatabaseBuilder.BuildAccessDb4o();
|
||||
logger.Info( "connection String : " + _connectionString );
|
||||
DbConfig.SaveConnectionString( _connectionString );
|
||||
logger.Info( "the connection String is resetted" );
|
||||
}
|
||||
|
||||
|
||||
|
||||
public List<String> GetTables() {
|
||||
return existTables;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Zhaizj.Framework.ORM;
|
||||
|
||||
namespace Zhaizj.Framework.Data {
|
||||
|
||||
|
||||
internal interface IDatabaseChecker {
|
||||
|
||||
String ConnectionString { get; set; }
|
||||
DatabaseType DatabaseType { get; set; }
|
||||
|
||||
void CheckDatabase();
|
||||
void CheckTable( MappingClass mapping, String db );
|
||||
|
||||
List<String> GetTables();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using Zhaizj.Framework.ORM;
|
||||
|
||||
namespace Zhaizj.Framework.Data {
|
||||
|
||||
|
||||
internal class MysqlDatabaseChecker : IDatabaseChecker {
|
||||
|
||||
private static readonly ILog logger = LogManager.GetLogger( typeof( MysqlDatabaseChecker ) );
|
||||
|
||||
private List<String> existTables = new List<String>();
|
||||
|
||||
private String _connectionString;
|
||||
|
||||
public String ConnectionString {
|
||||
get { return _connectionString; }
|
||||
set { _connectionString = value; }
|
||||
}
|
||||
|
||||
public DatabaseType DatabaseType {
|
||||
get { return DatabaseType.MySql; }
|
||||
set { }
|
||||
}
|
||||
|
||||
public void CheckDatabase() {
|
||||
|
||||
if (strUtil.IsNullOrEmpty( this._connectionString )) {
|
||||
throw new Exception( "connection string can not be empty" );
|
||||
}
|
||||
IDatabaseDialect dialect = DataFactory.GetDialect( DatabaseType.MySql );
|
||||
if (strUtil.IsNullOrEmpty( dialect.GetConnectionItem( this._connectionString, ConnectionItemType.Server ) )) {
|
||||
throw new Exception( "[mysql] server address is empty" );
|
||||
}
|
||||
if (strUtil.IsNullOrEmpty( dialect.GetConnectionItem( _connectionString, ConnectionItemType.Database ) )) {
|
||||
throw new Exception( "[mysql] database is empty" );
|
||||
}
|
||||
}
|
||||
|
||||
public void CheckTable( MappingClass mapping, String db ) {
|
||||
|
||||
logger.Info( "[mysql] begin check table" );
|
||||
IDbConnection connection = DataFactory.GetConnection( _connectionString, this.DatabaseType );
|
||||
connection.Open();
|
||||
|
||||
IDbCommand cmd = connection.CreateCommand();
|
||||
cmd.CommandText = "show tables";
|
||||
|
||||
IDataReader reader = cmd.ExecuteReader();
|
||||
|
||||
while (reader.Read()) {
|
||||
existTables.Add( reader[0].ToString() );
|
||||
logger.Info( "table found£º" + reader[0].ToString() );
|
||||
}
|
||||
|
||||
reader.Close();
|
||||
existTables = new MySqlTableBuilder().CheckMappingTableIsExist( cmd, db, existTables, mapping );
|
||||
|
||||
connection.Close();
|
||||
}
|
||||
|
||||
|
||||
public List<String> GetTables() {
|
||||
return existTables;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user