using OTSModelSharp.DTLBase;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OTSModelSharp.DTLBase
{
public class CSQLiteDB :IDBBase
{
//表结构
protected IDBTableBase m_sqlitetable;
//数据库连接地址
protected IDBStoreBase m_DBStore;
///
/// 初始换
///
/// 数据库地址
/// 表结构
public CSQLiteDB(IDBStoreBase a_dbStore, IDBTableBase a_table)
{
//dbStore =(CSQLiteDBStore) a_dbStore;
m_sqlitetable = a_table;
m_DBStore = a_dbStore;
}
///
/// 建表
///
/// 如果a_bClean删除表再建表
///
public bool Init(bool a_bClean = false)
{
if (IsDBExist())
{
if (a_bClean)
{
DeleteTable();
}
else
{
return true;
}
}
return CreateTable();
}
///
///建表
///
///
public bool CreateTable( bool a_bForce=false)
{
if (m_DBStore.IsTableExists(m_sqlitetable.GetTableName()))
{
if (a_bForce)
{
if (!DeleteTable())
{
return false;
}
}
else
{
return true;
}
}
// create table
string sSQLCommand = m_sqlitetable.GetCreateTableCommandString();
return m_DBStore.RunCommand(sSQLCommand);
}
///
///删除表
///
///
public bool DeleteTable()
{
return m_DBStore.DeleteTable(m_sqlitetable.GetTableName());
}
///
///移除表数据
///
///
public bool RemoveAllRows()
{
if (!m_DBStore. IsTableExists(m_sqlitetable. GetTableName()))
{
return true;
}
// create table
string sSQLCommand = m_sqlitetable.GetRemoveAllRowsCommandString();
return m_DBStore. RunCommand(sSQLCommand);
}
///
/// 判断表是否存在
///
///
public bool IsDBExist()
{
return m_DBStore.IsTableExists(m_sqlitetable.GetTableName());
}
///
/// 获取连接字符串
///
///
public IDBStoreBase GetDatastore()
{
return m_DBStore;
}
///
/// 获取表结构
///
///
///
/// 获取表所有信息
///
/// 排序字段
///
public IDBQueryBase GetTableQuery(string a_sOrderColumnName="")
{
return m_DBStore.QueryByTableName(m_sqlitetable.GetTableName(),a_sOrderColumnName);
}
public DataTable GetTableQueryForDataTable(string a_sOrderColumnName = "")
{
return m_DBStore.QueryByTableNameForDataTable(m_sqlitetable.GetTableName(), a_sOrderColumnName);
}
public IDBTableBase GetTableInfo()
{
return m_sqlitetable;
}
}
}