using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using IBatisNet.Common;
using IBatisNet.Common.Exceptions;
using IBatisNet.Common.Pagination;
using IBatisNet.Common.Utilities;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
using IBatisNet.DataMapper.Configuration.Statements;
using IBatisNet.DataMapper.Exceptions;
using IBatisNet.DataMapper.MappedStatements;
using IBatisNet.DataMapper.Scope;
using System.Data;
using System.Reflection;
using System.Data.Common;
using SiaSun.LMS.Model;
namespace SiaSun.LMS.Persistence
{
public class P_Base
{
public P_Base()
{
}
public ISqlMapper _sqlMap;
public string _sConfig;
#region Connection&Transaction
#endregion
#region Methods
///
/// 得到列表
///
/// 实体类型
/// 操作名称,对应xml中的Statement的id
/// 参数
///
protected IList ExecuteQueryForList(string statementName, object parameterObject)
{
try
{
return this._sqlMap.QueryForList(statementName, parameterObject);
}
catch (Exception e)
{
throw new DataMapperException("Error executing query '" + statementName + "' for list. Cause: " + e.Message, e);
}
}
public ObjectList ExecuteQueryForList(string statementName, object parameterObject)
{
try
{
IList ls = this._sqlMap.QueryForList(statementName, parameterObject);
ObjectList objlist = new ObjectList(ls);
return new Model.ObjectList(ls);
}
catch (Exception e)
{
throw new DataMapperException("Error executing query '" + statementName + "' for list. Cause: " + e.Message, e);
}
}
///
/// 得到指定数量的记录数
///
///
///
/// 参数
/// 跳过的记录数
/// 最大返回的记录数
///
protected IList ExecuteQueryForList(string statementName, object parameterObject, int skipResults, int maxResults)
{
try
{
return this._sqlMap.QueryForList(statementName, parameterObject, skipResults, maxResults);
}
catch (Exception e)
{
throw new DataMapperException("Error executing query '" + statementName + "' for list. Cause: " + e.Message, e);
}
}
protected IPaginatedList ExecuteQueryForPaginatedList(string statementName, object parameterObject, int pageSize)
{
try
{
return this._sqlMap.QueryForPaginatedList(statementName, parameterObject, pageSize);
}
catch (Exception e)
{
throw new IBatisNetException("Error executing query '" + statementName + "' for paginated list. Cause: " + e.Message, e);
}
}
///
/// 查询得到对象的一个实例
///
/// 对象type
/// 操作名
/// 参数
///
public T ExecuteQueryForObject(string statementName, object parameterObject)
{
try
{
return this._sqlMap.QueryForObject(statementName, parameterObject);
}
catch (Exception e)
{
throw new DataMapperException("Error executing query '" + statementName + "' for object. Cause: " + e.Message, e);
}
}
public ObjectT ExecuteQueryForObject(string statementName, object parameterObject)
{
try
{
object obj = this._sqlMap.QueryForObject(statementName, parameterObject);
return new ObjectT(obj);
}
catch (Exception e)
{
throw new DataMapperException("Error executing query '" + statementName + "' for object. Cause: " + e.Message, e);
}
}
///
/// 执行添加
///
/// 操作名
/// 参数
protected int ExecuteInsert(string statementName, object parameterObject)
{
object generatedKey = null;
try
{
generatedKey = this._sqlMap.Insert(statementName, parameterObject);
}
catch (Exception e)
{
throw new DataMapperException("Error executing query '" + statementName + "' for insert. Cause: " + e.Message, e);
}
return generatedKey == null ? 0 : 1;
}
///
/// 执行修改
///
/// 操作名
/// 参数
protected int ExecuteUpdate(string statementName, object parameterObject)
{
try
{
return this._sqlMap.Update(statementName, parameterObject);
}
catch (Exception e)
{
throw new DataMapperException("Error executing query '" + statementName + "' for update. Cause: " + e.Message, e);
}
}
///
/// 执行删除
///
/// 操作名
/// 参数
protected int ExecuteDelete(string statementName, object parameterObject)
{
try
{
return this._sqlMap.Delete(statementName, parameterObject);
}
catch (Exception e)
{
throw new DataMapperException("Error executing query '" + statementName + "' for delete. Cause: " + e.Message, e);
}
}
#endregion
#region 自定义
/// 获得Command
/// 获得Command
///
///
///
///
private IDbCommand GetCommand(IDalSession session, string statementName, object paramObject)
{
IStatement statement = this._sqlMap.GetMappedStatement(statementName).Statement;
IMappedStatement mapStatement = this._sqlMap.GetMappedStatement(statementName);
RequestScope request = statement.Sql.GetRequestScope(mapStatement, paramObject,session as ISqlMapSession);
mapStatement.PreparedCommand.Create(request, session as ISqlMapSession, statement, paramObject);
IDbCommand cmd = session.CreateCommand(CommandType.Text);
cmd.CommandText = request.IDbCommand.CommandText;
//return request.IDbCommand;
return cmd;
//return request.IDbCommand;
}
/// 获得DataSet
/// 获得DataSet
///
/// 语句ID
/// 语句所需要的参数
/// 得到的DataTable
public DataSet ExecuteQueryForDataSet(string statementName, object paramObject)
{
DataSet ds = new DataSet();
bool isSessionLocal = false;
IDalSession session = _sqlMap.LocalSession;
if (session == null)
{
session = new SqlMapSession(_sqlMap);
session.OpenConnection();
isSessionLocal = true;
}
IDbCommand cmd = this.GetCommand(session, statementName, paramObject);
//IDbCommand cmd = session.CreateCommand(CommandType.Text);
//cmd.CommandText = paramObject.ToString();
cmd.CommandTimeout = 5000;
try
{
cmd.Connection = session.Connection;
IDbDataAdapter adapter = session.CreateDataAdapter(cmd);
adapter.Fill(ds);
}
catch(Exception ex)
{
}
finally
{
if (isSessionLocal)
{
session.CloseConnection();
}
}
return ds;
}
/// 获得DataTable
/// 获得DataTable
///
/// 语句ID
/// 语句所需要的参数
/// 得到的DataTable
public DataTable ExecuteQueryForDataTable(string statementName, object paramObject)
{
DataTable dtResult = new DataTable();
try
{
dtResult = this.ExecuteQueryForDataSet(statementName, paramObject).Tables[0];
}
catch (Exception ex)
{
throw ex;
}
return dtResult;
}
///
/// 通过传入SQL进行查询,返回数据集
///
///
///
public DataSet GetDataSet(string strSQL)
{
DataSet dtResult = new DataSet();
try
{
dtResult = this.ExecuteQueryForDataSet("dynamicSQL", strSQL);
}
catch
{
}
return dtResult;
}
///
/// 通过传入SQL进行查询,返回数据集
///
///
///
public DataTable GetDataTable(string strSQL)
{
DataTable dtResult = new DataTable();
try
{
dtResult = this.ExecuteQueryForDataTable("dynamicSQL", strSQL);
}
catch
{
}
return dtResult;
}
/// 保存DataSet
/// 保存DataSet
///
///
///
///
///
public int SaveDataSet(DataSet ds, string statementName, object paramObject)
{
int iResult = 0;
bool isSessionLocal = false;
ISqlMapSession session = this._sqlMap.LocalSession;
if (session == null)
{
session = new SqlMapSession(_sqlMap);
session.OpenConnection();
isSessionLocal = true;
}
IDbCommand cmd = GetCommand(session, statementName, paramObject);
// IDbCommand cmd = session.CreateCommand(CommandType.Text);
try
{
cmd.Connection = session.Connection;
IDbDataAdapter adapter = session.CreateDataAdapter(cmd);
ConstructorInfo cif = _sqlMap.DataSource.DbProvider.CommandBuilderType.GetConstructor(new Type[] { adapter.GetType() });
cif.Invoke(new object[] { adapter });
//foreach (DataRow row in ds.Tables[0].Rows)
//{
//}
iResult = adapter.Update(ds);
// cmd.Cancel();
}
finally
{
if (isSessionLocal)
{
session.CloseConnection();
}
}
return iResult;
}
/// 保存DataTable
/// 保存DataTable
///
///
///
///
public int SaveDataTable(DataTable dt, string tablename)
{
int iResult;
DataSet ds;
if (null == dt.DataSet)
{
ds = new DataSet();
ds.Tables.Add(dt);
}
else
{
ds = dt.DataSet;
}
string tmpTableName = dt.TableName;
dt.TableName = "Table";
//for (int i = 0; i < dt.Rows.Count; i++)
//{
// if (DataRowState.Added == dt.Rows[i].RowState)
// {
// //dt.Rows[i][0] = this.GetPrimaryID(tablename, dt.Columns[0].ColumnName);
// }
//}
string sStament = "dynamicSQL";
string paramObject = " select * from " + tablename;
iResult = this.SaveDataSet(ds, sStament, paramObject);
dt.TableName = tmpTableName;
ds.Tables.Remove(dt);
return iResult;
}
/// 执行无结果语句
/// 执行无结果语句
///
///
///
///
public int ExecuteNonQuery(string SQL, string statementName)
{
int iResult = 0;
bool isSessionLocal = false;
IDalSession session = _sqlMap.LocalSession;
if (session == null)
{
session = new SqlMapSession(_sqlMap);
session.OpenConnection();
isSessionLocal = true;
}
IDbCommand cmd = session.CreateCommand(CommandType.Text);
try
{
cmd.CommandText = SQL;
iResult = cmd.ExecuteNonQuery();
}
finally
{
if (isSessionLocal)
{
session.CloseConnection();
}
}
return iResult;
}
public int GetPrimaryID(string tableName, string fieldName)
{
try
{
Hashtable ht = new Hashtable();
ht.Add("TABLENAME", tableName);
ht.Add("FIELDNAME", fieldName);
ht.Add("NEXTID", 0);
_sqlMap.QueryForList("GetTableID", ht);
return (int)ht["NEXTID"];
}
catch (Exception e)
{
throw (e);
//System.Threading.Thread.Sleep(3000);
//return this.GetPrimaryID(tableName, fieldName);
}
}
public int GetPrimaryID(string tableName)
{
try
{
string sSQL = string.Format(string.Format("select {0}_SEQ.nextval from dual", tableName));
return Convert.ToInt32(this.ExecuteQueryForDataTable("dynamicSQL", sSQL).Rows[0][0]);
}
catch (Exception ex)
{
throw (ex);
}
}
/// 初始化系统
/// 初始化系统
///
public void InitSystem()
{
try
{
_sqlMap.QueryForList("InitSystem", null);
}
catch (Exception e)
{
throw (e);
}
}
#endregion
#region 事物控制
public void BeginTransaction()
{
this._sqlMap.BeginTransaction();
}
public void BeginTransaction(bool bTrans)
{
if (!bTrans)
{
return;
}
this.BeginTransaction();
}
public void CommitTransaction()
{
this._sqlMap.CommitTransaction();
}
public void CommitTransaction(bool bTrans)
{
if (!bTrans)
{
return;
}
this.CommitTransaction();
}
public void RollBackTransaction()
{
this._sqlMap.RollBackTransaction();
}
public void RollBackTransaction(bool bTrans)
{
if (!bTrans)
{
return;
}
this.RollBackTransaction();
}
#endregion
#region Silverlight需要的基本方法
///
/// 执行命令并返回结果数
///
/// 命令
/// 结果数
public int ExecuteNonQuery(string command)
{
IDalSession session = _sqlMap.LocalSession;
if (session == null)
{
session = new SqlMapSession(_sqlMap);
session.OpenConnection();
}
try
{
IDbCommand cmd = session.CreateCommand(CommandType.Text);
cmd.CommandText = command;
return cmd.ExecuteNonQuery();
}
catch
{
return -1;
}
finally
{
session.CloseConnection();
}
}
///
/// 执行命令并返回首行首列内容
///
/// 命令
/// 结果
public object ExecuteScalar(string command)
{
int result = 0;
IDalSession session = _sqlMap.LocalSession;
if (session == null)
{
session = new SqlMapSession(_sqlMap);
session.OpenConnection();
}
try
{
IDbCommand cmd = session.CreateCommand(CommandType.Text);
cmd.CommandText = command;
return cmd.ExecuteScalar();
}
catch
{
return result;
}
finally
{
session.CloseConnection();
}
}
///
/// 执行命令并返回结果
///
/// 命令
/// 结果
public Dictionary GetEntityAsDictionary(string command)
{
Dictionary result = new Dictionary();
IDalSession session = _sqlMap.LocalSession;
if (session == null)
{
session = new SqlMapSession(_sqlMap);
session.OpenConnection();
}
try
{
IDbCommand cmd = session.CreateCommand(CommandType.Text);
cmd.CommandText = command;
using (IDataReader dr = cmd.ExecuteReader())
{
string[] fieldName = new string[dr.FieldCount];
for (int i = 0; i < dr.FieldCount; i++)
{
fieldName[i] = dr.GetName(i);
}
dr.Read();
for (int i = 0; i < dr.FieldCount; i++)
{
result.Add(fieldName[i], Convert.ToString(dr[i]));
}
}
return result;
}
catch
{
return result;
}
finally
{
session.CloseConnection();
}
}
///
/// 执行命令并返回结果集
///
/// 命令
/// 结果集
public List GetEntitiesAsStringList(string command)
{
List result = new List();
IDalSession session = _sqlMap.LocalSession;
if (session == null)
{
session = new SqlMapSession(_sqlMap);
session.OpenConnection();
}
try
{
IDbCommand cmd = session.CreateCommand(CommandType.Text);
cmd.CommandText = command;
using (IDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
string[] s = new string[dr.FieldCount];
for (int i = 0; i < dr.FieldCount; i++)
{
s[i] = Convert.ToString(dr[i]);
}
result.Add(s);
}
}
return result;
}
catch
{
return result;
}
finally
{
session.CloseConnection();
}
}
///
/// 执行命令并返回结果集
///
/// 命令
/// 结果集
public List> GetEntitiesAsDictionaryList(string command)
{
List> result = new List>();
IDalSession session = _sqlMap.LocalSession;
if (session == null)
{
session = new SqlMapSession(_sqlMap);
session.OpenConnection();
}
try
{
IDbCommand cmd = session.CreateCommand(CommandType.Text);
cmd.CommandText = command;
using (IDataReader dr = cmd.ExecuteReader())
{
string[] fieldName = new string[dr.FieldCount];
for (int i = 0; i < dr.FieldCount; i++)
{
fieldName[i] = dr.GetName(i);
}
while (dr.Read())
{
Dictionary row = new Dictionary();
for (int i = 0; i < dr.FieldCount; i++)
{
row.Add(fieldName[i], Convert.ToString(dr[i]));
}
result.Add(row);
}
}
return result;
}
catch
{
return result;
}
finally
{
session.CloseConnection();
}
}
#endregion
}
}