48 lines
1.4 KiB
C#
48 lines
1.4 KiB
C#
using System;
|
|
using System.Data;
|
|
using Zhaizj.Framework;
|
|
using Zhaizj.Framework.Data;
|
|
using Zhaizj.Framework.Log;
|
|
|
|
namespace Zhaizj.Framework.ORM.Operation {
|
|
|
|
internal class CountOperation {
|
|
|
|
private static readonly ILog logger = LogManager.GetLogger( typeof( CountOperation ) );
|
|
|
|
public static int Count( Type t ) {
|
|
return Count( t, "" );
|
|
}
|
|
|
|
public static int Count( Type t, String condition ) {
|
|
return Count( condition, Entity.GetInfo( t ) );
|
|
}
|
|
|
|
private static int Count( String condition, EntityInfo entityInfo ) {
|
|
String countSql;
|
|
int result = 0;
|
|
SqlBuilder builder = new SqlBuilder( entityInfo );
|
|
if (strUtil.IsNullOrEmpty( condition )) {
|
|
countSql = String.Format( "select count(*) from {0}", entityInfo.TableName );
|
|
}
|
|
else {
|
|
countSql = builder.GetCountSql( condition );
|
|
}
|
|
logger.Info( LoggerUtil.SqlPrefix+"[Count(String condition) Sql]:" + countSql );
|
|
IDbCommand command = DataFactory.GetCommand( countSql, DbContext.getConnection( entityInfo ) );
|
|
try {
|
|
result = cvt.ToInt( command.ExecuteScalar() );
|
|
}
|
|
catch (Exception exception) {
|
|
logger.Error( exception.Message );
|
|
throw exception;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|