Make your own free website on Tripod.com
EPolWS
DbSTable
Home
Account
ep_test.txt
SerializableBase
DbConnectionBase
EPolESForm
DbSTable
EmailFactory.cs
epmain
EPolES
DbVariableView.cs
ProfileView
EPolLOcal Main
Page Main Example
EPolWS EPolWS.asmx
EPolWS EPolWS.cs
CommandManager
CommandFactory
CommandFactoryBase
OrganisationView
EPolConsole Main
DbEPBase.cs
DbRecordArray
DbTable

using System; using System.Data; using System.IO; using System.Windows.Forms; using System.Collections; using System.Diagnostics; using System.Globalization; using System.Text; using System.Threading; using EXCommon; //using EXAttribute; using EXDb97; //###2 namespace EPol { ///------------------------------------------------------------------------------------------------------------------------- ///

/// Summary description for DbMultiLanguageTable. /// ///------------------------------------------------------------------------------------------------------------------------- public class CDbJoinOnSIndexWithBackupView : CDbJoinOnSIndexView { public CDbJoinOnSIndexWithBackupView(string sMasterPrefixe, CDbTreeTable oMasterTable, string sSlavePrefixe, string sSlaveTableName) : base(sMasterPrefixe, oMasterTable, new CDbTableView(sSlavePrefixe, new CDbMemeArchiveTable(sSlaveTableName, oMasterTable.sDatabaseName))) { } public CDbMemeArchiveTable oGetSlaveBackupTable { get { return (CDbMemeArchiveTable)(((CDbTableView)m_oSlaveView).oGetTable); } } } ///------------------------------------------------------------------------------------------------------------------------- /// /// Summary description for DbMultiLanguageTable. /// ///------------------------------------------------------------------------------------------------------------------------- public class CDbJoinOnSIndexView : CDbJoinView { public CDbJoinOnSIndexView(string sMasterPrefixe, string sMasterTableName, string sSlavePrefixe, string sSlaveTableName, string sDatabaseName) : base(new CDbTableView(sMasterPrefixe, new CDbTreeTable(sMasterTableName, sDatabaseName)), new CDbTableView(sSlavePrefixe, new CDbSTable(sSlaveTableName, sDatabaseName))) { oGetMasterTable.vPutIndexColumnsTo(oGetSlaveTable); vAddLinkIndex(sMasterPrefixe, MASTER_INDEX_FIELD, sSlavePrefixe, MASTER_INDEX_FIELD); vAddLinkIndex(sMasterPrefixe, MASTER_SUB_INDEX_FIELD, sSlavePrefixe, MASTER_SUB_INDEX_FIELD); } public CDbJoinOnSIndexView(string sMasterPrefixe, CDbTreeTable oMasterTable, string sSlavePrefixe, string sSlaveTableName) : base(new CDbTableView(sMasterPrefixe, oMasterTable), new CDbTableView(sSlavePrefixe, new CDbSTable(sSlaveTableName, oMasterTable.sDatabaseName))) { oGetMasterTable.vPutIndexColumnsTo(oGetSlaveTable); vAddLinkIndex(sMasterPrefixe, MASTER_INDEX_FIELD, sSlavePrefixe, MASTER_INDEX_FIELD); vAddLinkIndex(sMasterPrefixe, MASTER_SUB_INDEX_FIELD, sSlavePrefixe, MASTER_SUB_INDEX_FIELD); } public CDbJoinOnSIndexView(string sMasterPrefixe, CDbTreeTable oMasterTable, string sSlavePrefixe, string sSlaveTableName, string sSlaveIndexFieldName, string sSlaveSubIndexFieldName) : base(new CDbTableView(sMasterPrefixe, oMasterTable), new CDbTableView(sSlavePrefixe, new CDbSTable(sSlaveTableName, oMasterTable.sDatabaseName))) { oGetMasterTable.vPutIndexColumnsTo(oGetSlaveTable); vAddLinkIndex(sMasterPrefixe, MASTER_INDEX_FIELD, sSlavePrefixe, sSlaveIndexFieldName); vAddLinkIndex(sMasterPrefixe, MASTER_SUB_INDEX_FIELD, sSlavePrefixe, sSlaveSubIndexFieldName); } protected CDbJoinOnSIndexView(string sMasterPrefixe, CDbSIndexTable oMasterTable, CDbTableView oSlaveView) : base(new CDbTableView(sMasterPrefixe, oMasterTable), oSlaveView) { oGetMasterTable.vPutIndexColumnsTo(oSlaveView.oGetTable); vAddLinkIndex(sMasterPrefixe, MASTER_INDEX_FIELD, oSlaveView.sqlPrefixe, MASTER_INDEX_FIELD); vAddLinkIndex(sMasterPrefixe, MASTER_SUB_INDEX_FIELD, oSlaveView.sqlPrefixe, MASTER_SUB_INDEX_FIELD); } public virtual ec dAddAttribute(SIndex oMeme, P dAttributeType, object oValue) { ec dRC = ec.ST_SUCCEED; try { oGetSlaveTable.vAddValue(oMeme); if (dAttributeType == P.INTEGER_TYPE) { int dValue; dValue = Convert.ToInt32(oValue); oGetSlaveTable.vAddValue(dValue); } else if (dAttributeType == P.TITLE_AND_TEXT_ATTRIBUTE_TYPE) { STitleAndText oTitleAndText = (STitleAndText)oValue; oGetSlaveTable.vAddValue(oTitleAndText.sTitle); oGetSlaveTable.vAddValue(oTitleAndText.sText); } else { oGetSlaveTable.vAddValue(oValue.ToString()); } dRC = oGetSlaveTable.dInsertIntoTable(); } catch { EPAssert(false, "Not an integer = " + oValue.ToString()); return ec.ST_ATTRIBUTE_CONVERTION_ERROR_TOWARD_INTEGER; } return dRC == ec.ST_SUCCEED ? ec.ST_SUCCEED : ec.ST_ATTRIBUTE_CAN_NOT_INSERT_THE_ATTRIBUTE; } public CDbSTable oGetSlaveTable { get { return ((CDbTableView)m_oSlaveView).oGetTable; } } public CDbTableView oGetMasterView { get { return (CDbTableView)m_oMasterView; } } public CDbTableView oGetSlaveView { get { return (CDbTableView)m_oSlaveView; } } public CDbTreeTable oGetMasterTable { get { return (CDbTreeTable)((CDbTableView)m_oMasterView).oGetTable; } } public void vAddFilter(P dEntityClassFilter, string sFieldName) { oGetMasterTable.vAddFilter(dEntityClassFilter, sFieldName); } public virtual ec dUpdate(SIndex oIndex, string sFieldName, object oValue) { ec dRC = oGetSlaveTable.dUpdate(sFieldName, oValue.ToString(), sqlIsIndex(oIndex)); return dRC== ec.ST_SUCCEED ? ec.ST_SUCCEED : ec.ST_ATTRIBUTE_CAN_NOT_UPDATE_THE_ATTRIBUTE; } public void vResetDate(SIndex oIndex) { oGetMasterTable.dResetDate(sqlIsIndex(oIndex)); } /* public bool bAddNewValues(int dMemeIndex, params object[] oValueParameterArray) { SIndex oIndex; oIndex.dIndex = dMemeIndex; oIndex.dSubIndex = oGetSlaveTable.dGetMax(MASTER_SUB_INDEX_FIELD, oGetSlaveTable.sFieldEqual(MASTER_INDEX_FIELD, oIndex.dIndex)); return bAddNewValues(oIndex.dIndex, oValueParameterArray); }*/ public int dGetRecordCount() { this.bAddDisplayColumn(oGetMasterView.sqlPrefixe, MASTER_INDEX_FIELD); this.bAddDisplayColumn(oGetMasterView.sqlPrefixe, MASTER_SUB_INDEX_FIELD); int dCount; dGetRecordCount(this.sqlCount(), out dCount); // EPAssert(dCount >= 0, "Bad record count sql = " + this.sqlCount()); return dCount; } public ec dGetSelect(int dMaxItems, out SIndex[] oIndexArray) { ArrayList oOutputArray; this.bAddDisplayColumn(oGetMasterView.sqlPrefixe, MASTER_INDEX_FIELD); this.bAddDisplayColumn(oGetMasterView.sqlPrefixe, MASTER_SUB_INDEX_FIELD); oIndexArray = null; ec dRC; dRC = base.dGetSelect( dMaxItems, out oOutputArray); if (dRC == ec.ST_SUCCEED) { dRC = dConvert(oOutputArray, out oIndexArray); } return dRC; } public ec dGetSelect(SIndex oIndex, out ArrayList oOutputArray, string[] sOutputFieldMasterViewArray, string[] sOutputFieldSlaveViewArray) { vResetSelect(); if (sOutputFieldMasterViewArray != null) { for (int i = 0; i < sOutputFieldMasterViewArray.Length; i++) { bAddDisplayColumn(oGetMasterView.sqlPrefixe, sOutputFieldMasterViewArray[i]); } } if (sOutputFieldSlaveViewArray != null) { for (int i = 0; i < sOutputFieldSlaveViewArray.Length; i++) { bAddDisplayColumn(oGetSlaveView.sqlPrefixe, sOutputFieldSlaveViewArray[i]); } } this.bAddWhere(oGetMasterView.sqlPrefixe, oIndex); return base.dGetSelect(out oOutputArray); } public string sqlIsIndex(SIndex oIndex) { return oGetMasterTable.sFieldEqual(MASTER_INDEX_FIELD, oIndex.dIndex) + AND + oGetMasterTable.sFieldEqual(MASTER_SUB_INDEX_FIELD, oIndex.dSubIndex); } } ///------------------------------------------------------------------------------------------------------------------------- /// /// Summary description for DbMultiLanguageTable. /// ///------------------------------------------------------------------------------------------------------------------------- public class CDbJoinView : CDb2JoinView { public CDbJoinView(CDbView oMasterView, CDbView oSlaveView) : base(oMasterView, oSlaveView) { } public override string sqlGetIndexJoin(string sPrefixe) { string sqlJoin = sPrefixe == null ? m_sqlIndexLink : sAddPrefixe(m_sqlIndexLink, sPrefixe, '_'); if (m_oMasterView.sqlGetIndexJoin(sPrefixe) != null) { if (sqlJoin != null) sqlJoin += AND; sqlJoin += m_oMasterView.sqlGetIndexJoin(sPrefixe); } if (m_oSlaveView.sqlGetIndexJoin(sPrefixe) != null) { if (sqlJoin != null) sqlJoin += AND; sqlJoin += m_oSlaveView.sqlGetIndexJoin(sPrefixe); } CTrace.i().vWriteLn(ENMessage.DbS," sqlJoin = " + sqlJoin); return sqlJoin; } string m_sqlIndexLink; public override bool bAddWhere(string sPrefixe, string sColumnName, ENSQLRelation enRelation, string sValue) { if (m_oMasterView.bAddWhere(sPrefixe, sColumnName, enRelation, sValue)) { return true; } else if (m_oSlaveView.bAddWhere(sPrefixe, sColumnName, enRelation, sValue)) { return true; } return false; } public override bool bAddWhere(string sPrefixe1, string sColumnName1, ENSQLRelation enRelation, string sPrefixe2, string sColumnName2) { if (m_oMasterView.bAddWhere( sPrefixe1, sColumnName1, enRelation, sPrefixe2, sColumnName2)) { return true; } else if (m_oSlaveView.bAddWhere(sPrefixe1, sColumnName1, enRelation, sPrefixe2, sColumnName2)) { return true; } return false; } public override bool bAddOrderBy(string sPrefixe, string sColumnName) { if (m_oMasterView.bAddOrderBy(sPrefixe, sColumnName)) { return true; } else if (m_oSlaveView.bAddOrderBy(sPrefixe, sColumnName)) { return true; } return false; } public override string sqlSelect(string sPrefixe, string sColumn, string sWhere) { string sSql= m_oMasterView.sqlSelect(sPrefixe, sColumn, sWhere); if (sSql == null) { sSql = m_oSlaveView.sqlSelect(sPrefixe, sColumn, sWhere); } return sSql; } public override bool bAddWhere(string sPrefixe, string sSql) { bool bRet = m_oMasterView.bAddWhere(sPrefixe, sSql); if (bRet) { bRet = m_oSlaveView.bAddWhere(sPrefixe, sSql); } return bRet; } public override bool bAddDisplayColumn(string sPrefixe, STRDisplayColumn sColumnName) { if (m_oMasterView.bAddDisplayColumn(sPrefixe, sColumnName)) { return true; } else if (m_oSlaveView.bAddDisplayColumn(sPrefixe, sColumnName)) { return true; } return false; } public override string sGetTypeOf(string sFieldName) { string sType = m_oMasterView.sGetTypeOf(sFieldName); if ( sType != null) { return sType; } return m_oSlaveView.sGetTypeOf(sFieldName); } public override int dGetDisplayFieldCount { get { return m_oMasterView.dGetDisplayFieldCount + m_oSlaveView.dGetDisplayFieldCount; } } public override STRDisplayColumn GetFieldName(int i) { if (i >= 0 && i < m_oMasterView.dGetDisplayFieldCount) { return m_oMasterView.GetFieldName(i); } else if (i >= m_oMasterView.dGetDisplayFieldCount && i < dGetDisplayFieldCount) { return m_oSlaveView.GetFieldName(i - m_oMasterView.dGetDisplayFieldCount); } else { return new STRDisplayColumn(); } } public override void vAddLinkIndex(string sMasterPrefixe, string sMasterFieldName, string sSlavePrefixe, string sSlaveFieldName) { if (m_sqlIndexLink != null) { m_sqlIndexLink += AND; } m_sqlIndexLink += sMasterPrefixe + "." + sMasterFieldName; m_sqlIndexLink += "="; m_sqlIndexLink += sSlavePrefixe + "." + sSlaveFieldName; } public override string sqlWhere { get { string sWhere = null; if (m_oWhereArrayList.Count != 0) { sWhere += sqlConcat(m_oWhereArrayList, AND); } if (m_oMasterView.sqlWhere != null) { if (sWhere != null) sWhere += AND; sWhere += m_oMasterView.sqlWhere; } if (m_oSlaveView.sqlWhere != null) { if (sWhere != null) sWhere += AND; sWhere += m_oSlaveView.sqlWhere; } return sWhere; } } public override string sqlOrderBy { get { string sOrderBy = null; if (m_oOrderByArrayList.Count != 0) { sOrderBy += sqlConcat(m_oOrderByArrayList, ", "); } return sOrderBy; } } public override string sqlFrom { get { return m_oMasterView.sqlFrom + ", " + m_oSlaveView.sqlFrom; } } public override string sqlReflexiveFrom { get { return m_oMasterView.sqlReflexiveFrom + ", " + m_oSlaveView.sqlReflexiveFrom; } } public string sqlReflexive(string sFieldList) { string sSelect = "SELECT " + sFieldList + " FROM " + sqlFrom + ", " + sqlReflexiveFrom + " where "; CTrace.i().vWriteLn(ENMessage.DbS,"sSelect = " + sSelect); if (sAddWhere() != null) { sSelect += sAddWhere(); } if (sqlGetIndexJoin(REFLEXIVE_PREFIXE) != null) { sSelect += " and "; sSelect += sqlGetIndexJoin(REFLEXIVE_PREFIXE); } CTrace.i().vWriteLn(ENMessage.DbS,"sSelect = " + sSelect); return sSelect; } public ec dGetReflexiveSelect(out ArrayList oOutputArray) { return dGetSelect(sqlReflexive(sGetFieldList), out oOutputArray); } public string csDumpReflexiveTable() { return csDump(sqlReflexive("*")); } public bool bAddReflexiveWhere(string sPrefixe, string sColumnName, int dValue) { bAddWhere(sPrefixe, sColumnName, dValue); return bAddWhere(REFLEXIVE_PREFIXE + "_" + sPrefixe, sColumnName, ENSQLRelation.neq, dValue); } } ///------------------------------------------------------------------------------------------------------------------------- /// /// Summary description for DbMultiLanguageTable. /// ///------------------------------------------------------------------------------------------------------------------------- public abstract class CDb2JoinView : CDbView { public CDb2JoinView(CDbView oMasterView, CDbView oSlaveView) : base(oMasterView.sDatabaseName) { m_oMasterView = oMasterView; m_oSlaveView = oSlaveView; } protected CDbView m_oMasterView, m_oSlaveView; public abstract void vAddLinkIndex(string sMasterPrefixe, string sMasterFieldName, string sSlavePrefixe, string sSlaveFieldName); public override void vCleanAllData() { m_oMasterView.vCleanAllData(); m_oSlaveView.vCleanAllData(); } public override string sDatabaseName { get { return m_oMasterView.sDatabaseName; } } public override bool bIsTableExist() { return m_oSlaveView.bIsTableExist() && m_oMasterView.bIsTableExist(); } public override void vResetSelect() { m_dColPos = 0; m_oMasterView.vResetSelect(); m_oSlaveView.vResetSelect(); } public override CDbView GetMasterView { get { return m_oMasterView; } } public CDbView GetSlaveView { get { return m_oSlaveView; } } public override string sGetMasterTableName { get { return m_oMasterView.sGetMasterTableName; } } } ///------------------------------------------------------------------------------------------------------------------------- /// /// Summary description for DbMultiLanguageTable. /// ///------------------------------------------------------------------------------------------------------------------------- public abstract class CDbMultiSlaveJoinView : CDbView { public CDbMultiSlaveJoinView(CDbView oMasterView, CDbTableViewArray oSlaveViewArray) : base(oMasterView.sDatabaseName) { m_oMasterView = oMasterView; m_oSlaveViewArray = oSlaveViewArray; } protected CDbView m_oMasterView; protected CDbTableViewArray m_oSlaveViewArray; public const string ALL_SLAVE_VIEW = "AllSlaveView"; public abstract void vAddLinkIndex(string sMasterPrefixe, string sMasterFieldName, string sSlavePrefixe, string sSlaveFieldName); public CDbTableViewArray GetSlaveArrayView { get { return m_oSlaveViewArray; } } protected CDbView GetSlaveView(string sPrefixe) { return m_oSlaveViewArray.GetView(sPrefixe); } public int dSlaveViewCount { get { return m_oSlaveViewArray.Length; } } public override void vCleanAllData() { m_oMasterView.vCleanAllData(); for (int i = 0; i < m_oSlaveViewArray.Length; i++) { m_oSlaveViewArray[i].vCleanAllData(); } } public override string sDatabaseName { get { return m_oMasterView.sDatabaseName; } } public override bool bIsTableExist() { for (int i = 0; i < m_oSlaveViewArray.Length; i++) { if (m_oSlaveViewArray[i].bIsTableExist()== false) { return false; } } return m_oMasterView.bIsTableExist(); } public override void vResetSelect() { m_dColPos = 0; m_oMasterView.vResetSelect(); for (int i = 0; i < m_oSlaveViewArray.Length; i++) { m_oSlaveViewArray[i].vResetSelect(); } } public override CDbView GetMasterView { get { return m_oMasterView; } } public CDbView GetSlaveView(int i) { return m_oSlaveViewArray[i]; } public override string sGetMasterTableName { get { return m_oMasterView.sGetMasterTableName; } } } ///------------------------------------------------------------------------------------------------------------------------- public class CDbTableViewArray : CDbEPBase { public CDbTableViewArray(string sPrefixe, string sTableBase, int dTableCount, string sDatabaseName) { m_sPrefixe = sPrefixe; m_oViewArrayList = new ArrayList(); for (int i = 0; i < dTableCount; i++) { m_oViewArrayList.Add(new CDbTableView(sGetPrefixe(i), new CDbSTable(sTableBase + i, sDatabaseName))); } } public CDbTableViewArray(string sPrefixe, params CDbSTable[] oTableArray) { m_sPrefixe = sPrefixe; m_oViewArrayList = new ArrayList(); for (int i = 0; i < oTableArray.Length; i++) { m_oViewArrayList.Add(new CDbTableView(sGetPrefixe(i), oTableArray[i])); } } public string m_sPrefixe; ArrayList m_oViewArrayList; public int dGetNext(string sKeyColumn, string sWhere) { int dNextMax=1; for(int i=0; i < Length; i++ ) { int dNext= GetTable(i).dGetNext(sKeyColumn, sWhere); if (dNext > dNextMax) { dNextMax = dNext; } } return dNextMax; } static public int dGetIndex(string sSlavePrefixe) { int dIndex = sSlavePrefixe.IndexOf('_'); if (dIndex <= 0) { return NOT_USED; } return Convert.ToInt32(sSlavePrefixe.Substring(dIndex + 1)); } public string sGetPrefixe(int i) { return m_sPrefixe + "_" + i; } public CDbTableView GetView(string sSlavePrefixe) { int dSuffixeIndex = dGetIndex(sSlavePrefixe); if (IsUsed(dSuffixeIndex)) { return (CDbTableView)m_oViewArrayList[dSuffixeIndex]; } return null; } public CDbSTable GetTable(string sSlavePrefixe) { CDbTableView oTableView= GetView( sSlavePrefixe); if (oTableView != null) { return oTableView.oGetTable; } return null; } public CDbSTable GetTable(int dSuffixeIndex) { CDbTableView oTableView= (CDbTableView)m_oViewArrayList[dSuffixeIndex]; if (oTableView != null) { return oTableView.oGetTable; } return null; } public virtual void vAddColumn(string strFieldName, string strType, bool bPrimaryKey) { for (int i = 0; i < m_oViewArrayList.Count; i++) { GetTable(i).vAddColumn(strFieldName, strType, bPrimaryKey); } } public CDbTableView this[int dIndex] { get { return (CDbTableView)m_oViewArrayList[dIndex]; } } public int Length { get { return m_oViewArrayList.Count; } } } ///------------------------------------------------------------------------------------------------------------------------- public class CDbMultiJoinView : CDbMultiSlaveJoinView { public CDbMultiJoinView(CDbView oMasterView, CDbTableViewArray oSlaveViewArray) : base(oMasterView, oSlaveViewArray) { m_sqlIndexLink = new string[oSlaveViewArray.Length]; for (int i = 0; i < m_sqlIndexLink.Length; i++) { m_sqlIndexLink[i] = null; } } string[] m_sqlIndexLink; public override string sqlGetIndexJoin(string sPrefixe) { int dIdx = CDbTableViewArray.dGetIndex(sPrefixe); string sqlJoin; // sqlJoin= sAddPrefixe(m_sqlIndexLink[dIdx], sPrefixe, '_'); sqlJoin = m_sqlIndexLink[dIdx]; if (m_oMasterView.sqlGetIndexJoin(sPrefixe) != null) { if (sqlJoin != null) sqlJoin += AND; sqlJoin += m_oMasterView.sqlGetIndexJoin(sPrefixe); } if (GetSlaveView(sPrefixe).sqlGetIndexJoin(sPrefixe) != null) { if (sqlJoin != null) sqlJoin += AND; sqlJoin += GetSlaveView(sPrefixe).sqlGetIndexJoin(sPrefixe); } CTrace.i().vWriteLn(ENMessage.DbMultiJoin, " sqlJoin = " + sqlJoin); return sqlJoin; } protected override string sAddWhere() { string sWhere = sqlWhere; for (int i = 0; i < m_sqlIndexLink.Length; i++) { if (sqlGetIndexJoin(m_oSlaveViewArray.sGetPrefixe(i)) != null) { if (sWhere != null && sWhere.Length > 0) sWhere += AND; sWhere += sqlGetIndexJoin(m_oSlaveViewArray.sGetPrefixe(i)); } } CTrace.i().vWriteLn(ENMessage.DbMultiSlaveView,"sAddWhere() sWhere = " + sWhere); return sWhere; } public override bool bAddWhere(string sPrefixe, string sColumnName, ENSQLRelation enRelation, string sValue) { if (m_oMasterView.bAddWhere(sPrefixe, sColumnName, enRelation, sValue)) { return true; } else { if (GetSlaveView(sPrefixe).bAddWhere(sPrefixe, sColumnName, enRelation, sValue)) { return true; } } return false; } public override bool bAddWhere(string sPrefixe, string sColumnName1, ENSQLRelation enRelation, string sPrefixe2, string sColumnName2) { if (m_oMasterView.bAddWhere(sPrefixe, sColumnName1, enRelation, sPrefixe2, sColumnName2)) { return true; } else if (GetSlaveView(sPrefixe).bAddWhere(sPrefixe, sColumnName1, enRelation, sPrefixe2, sColumnName2)) { return true; } return false; } public override bool bAddOrderBy(string sPrefixe, string sColumnName) { if (m_oMasterView.bAddOrderBy(sPrefixe, sColumnName)) { return true; } else if (GetSlaveView(sPrefixe).bAddOrderBy(sPrefixe, sColumnName)) { return true; } return false; } public override string sqlSelect(string sPrefixe, string sColumn, string sWhere) { string sSql = m_oMasterView.sqlSelect(sPrefixe, sColumn, sWhere); if (sSql == null) { sSql = GetSlaveView(sPrefixe).sqlSelect(sPrefixe, sColumn, sWhere); } return sSql; } public override bool bAddWhere(string sPrefixe, string sSql) { bool bRet = m_oMasterView.bAddWhere(sPrefixe, sSql); if (bRet) { bRet = GetSlaveView(sPrefixe).bAddWhere(sPrefixe, sSql); } return bRet; } public override bool bAddDisplayColumn(string sPrefixe, STRDisplayColumn sColumnName) { if (m_oMasterView.bAddDisplayColumn(sPrefixe, sColumnName)) { return true; } else if (GetSlaveView(sPrefixe).bAddDisplayColumn(sPrefixe, sColumnName)) { return true; } return false; } public override string sGetTypeOf(string sFieldName) { string sType = m_oMasterView.sGetTypeOf(sFieldName); if (sType != null) { return sType; } for (int i = 0; i < dSlaveViewCount; i++) { sType = m_oSlaveViewArray[i].sGetTypeOf(sFieldName); if (sType != null) { return sType; } } return null; } public override int dGetDisplayFieldCount { get { int dCount = m_oMasterView.dGetDisplayFieldCount; for (int i = 0; i < dSlaveViewCount; i++) { dCount += m_oSlaveViewArray[i].dGetDisplayFieldCount; } return dCount; } } public override STRDisplayColumn GetFieldName(int i) { if (i >= 0 && i < m_oMasterView.dGetDisplayFieldCount) { return m_oMasterView.GetFieldName(i); } else if (i >= m_oMasterView.dGetDisplayFieldCount && i < dGetDisplayFieldCount) { int dFieldDisplayCount = m_oMasterView.dGetDisplayFieldCount; for (int j = 0; j < dSlaveViewCount; j++) { if ((i - dFieldDisplayCount) > m_oSlaveViewArray[j].dGetDisplayFieldCount) { dFieldDisplayCount += m_oSlaveViewArray[j].dGetDisplayFieldCount; } else { return m_oSlaveViewArray[j].GetFieldName(i - dFieldDisplayCount); } } return new STRDisplayColumn(); } else { return new STRDisplayColumn(); } } public override void vAddLinkIndex(string sMasterPrefixe, string sMasterFieldName, string sSlavePrefixe, string sSlaveFieldName) { for (int i = 0; i < this.m_oSlaveViewArray.Length; i++) { if (m_sqlIndexLink[i] != null) { m_sqlIndexLink[i] += AND; } m_sqlIndexLink[i] += sMasterPrefixe + "." + sMasterFieldName; m_sqlIndexLink[i] += "="; m_sqlIndexLink[i] += m_oSlaveViewArray.sGetPrefixe(i) + "." + sSlaveFieldName; } } public bool bAddWhere(string sPrefixe, CEntityArray iValueArray) { bool bRet = false; if (iValueArray != null) { if (iValueArray.Length == 0) return true; for (int i = 0; i < iValueArray.Length; i++) { bRet = bAddWhere(sPrefixe, iValueArray[i]); } } return bRet; } public virtual bool bAddWhere(string sPrefixe, SEntity eValue) { if (eValue.bEqualTo(P.NONE)) { return bAddWhere(sPrefixe, ENTITY_CLASS_INDEX_FIELD, ENSQLRelation.neq, eValue.dClass); } else if (eValue.bEqualTo(P.ALL)) { return bAddWhere(sPrefixe, ENTITY_CLASS_INDEX_FIELD, ENSQLRelation.eq, eValue.dClass); } else { if (eValue.bIsType(P.P_TYPE)) { if (eValue.bExact) { bAddWhere(sPrefixe, ENTITY_CLASS_INDEX_FIELD, ENSQLRelation.eq, eValue.dClass); return bAddWhere(sPrefixe, ENTITY_INDEX_FIELD, ENSQLRelation.eq, eValue.sql); } else { string sSqlNotExist = sqlNotExist(sPrefixe, " 1 ", ENTITY_CLASS_INDEX_FIELD + " = " + eValue.dClass + AND + ENTITY_INDEX_FIELD + " <> " + eValue.sql); return bAddWhere(sPrefixe,sSqlNotExist); } } else if (eValue.bIsType(P.SINDEX_TYPE)) { if (eValue.bExact) { bAddWhere(sPrefixe, ENTITY_CLASS_INDEX_FIELD, ENSQLRelation.eq, eValue.dClass); return bAddWhere(sPrefixe, ENTITY_INDEX_FIELD, ENSQLRelation.eq, eValue.sql); } else { string sSqlNotExist = sqlNotExist(sPrefixe, " 1 ", ENTITY_CLASS_INDEX_FIELD + " = " + eValue.dClass + AND + "(" + INDEX_FIELD + " <> " + ((SIndex)eValue.Instance).dIndex + OR + SUB_INDEX_FIELD + " <> " + ((SIndex)eValue.Instance).dSubIndex + ") " ); return bAddWhere(sPrefixe,sSqlNotExist); } } else { CTrace.i().vWriteLn(ENMessage.Error, "bAddWhere type is not dealed " + eValue.Instance.ToString()); return false; } } } public override string sqlWhere { get { string sWhere = null; if (m_oWhereArrayList.Count != 0) { sWhere += sqlConcat(m_oWhereArrayList, AND); } if (m_oMasterView.sqlWhere != null) { if (sWhere != null) sWhere += AND; sWhere += m_oMasterView.sqlWhere; } for (int i = 0; i < dSlaveViewCount; i++) { if (m_oSlaveViewArray[i].sqlWhere != null) { if (sWhere != null) sWhere += AND; sWhere += m_oSlaveViewArray[i].sqlWhere; } } return sWhere; } } public override string sqlOrderBy { get { string sOrderBy = null; if (m_oOrderByArrayList.Count != 0) { sOrderBy += sqlConcat(m_oOrderByArrayList, ", "); } return sOrderBy; } } public override string sqlFrom { get { string _sqlFrom = m_oMasterView.sqlFrom; for (int i = 0; i < dSlaveViewCount; i++) { if (_sqlFrom != null) _sqlFrom += ", "; _sqlFrom += m_oSlaveViewArray[i].sqlFrom; } return _sqlFrom; } } public override string sqlReflexiveFrom { get { string _sqlFrom = m_oMasterView.sqlReflexiveFrom; for (int i = 0; i < dSlaveViewCount; i++) { if (_sqlFrom != null) _sqlFrom += ", "; _sqlFrom += m_oSlaveViewArray[i].sqlReflexiveFrom; } return _sqlFrom; } } public string sqlReflexive(string sFieldList) { string sSelect = "SELECT " + sFieldList + " FROM " + sqlFrom + ", " + sqlReflexiveFrom + " where "; CTrace.i().vWriteLn(ENMessage.DbS, "sSelect = " + sSelect); if (sAddWhere() != null) { sSelect += sAddWhere(); } if (sqlGetIndexJoin(REFLEXIVE_PREFIXE) != null) { sSelect += " and "; sSelect += sqlGetIndexJoin(REFLEXIVE_PREFIXE); } CTrace.i().vWriteLn(ENMessage.DbS, "sSelect = " + sSelect); return sSelect; } public ec dGetReflexiveSelect(out ArrayList oOutputArray) { return dGetSelect(sqlReflexive(sGetFieldList), out oOutputArray); } public string csDumpReflexiveTable() { return csDump(sqlReflexive("*")); } public bool bAddReflexiveWhere(string sPrefixe, string sColumnName, int dValue) { bAddWhere(sPrefixe, sColumnName, dValue); return bAddWhere(REFLEXIVE_PREFIXE + "_" + sPrefixe, sColumnName, ENSQLRelation.neq, dValue); } } ///---------------------------------------------------------------------------------------------------------------------------------------------- /// /// Summary description for DbMultiLanguageTable. /// ///---------------------------------------------------------------------------------------------------------------------------------------------- public class CDbTableView : CDbView { public CDbTableView(string sPrefixe, CDbSTable oTable) : base(oTable.sDatabaseName) { m_sPrefixe = sPrefixe; m_oTable = oTable; m_oFieldNameArrayList = new ArrayList(); } public CDbSTable oGetTable { get { return m_oTable; } } public override bool bIsTableExist() { return m_oTable.IsTableExist; } CDbSTable m_oTable; string m_sPrefixe; ArrayList m_oFieldNameArrayList; public override void vResetSelect() { m_dColPos = 0; m_oFieldNameArrayList.Clear(); m_oWhereArrayList.Clear(); m_oOrderByArrayList.Clear(); } public override string sDatabaseName { get { return m_oTable.sDatabaseName; } } public override int dGetDisplayFieldCount { get { return m_oFieldNameArrayList.Count; } } public override STRDisplayColumn GetFieldName(int i) { return (STRDisplayColumn)m_oFieldNameArrayList[i]; } public override string sqlFrom { get { return m_oTable.Name + " " + m_sPrefixe; } } public override string sqlReflexiveFrom { get { return m_oTable.Name + " " + REFLEXIVE_PREFIXE + '_' + m_sPrefixe; } } public string sqlPrefixe { get { return m_sPrefixe == "" ? null : m_sPrefixe; } } public override string sGetMasterTableName { get { return m_oTable.Name; } } public override string sqlWhere { get { if (m_oWhereArrayList.Count == 0) return null; return sqlConcat(m_oWhereArrayList, AND); } } public override string sqlOrderBy { get { if (m_oOrderByArrayList.Count == 0) return null; return sqlConcat(m_oOrderByArrayList, ", "); } } public override void vCleanAllData() { m_oTable.dDropTable(); } public void vAddColumn(string strFieldName, string strType) { m_oTable.vAddColumn(strFieldName, strType, false); } public override string sqlSelect(string sPrefixe, string sColumn, string sWhere) { if (this.sqlPrefixe == sPrefixe) { return "select " + this.sqlPrefixe + "." + sColumn + " from " + this.sqlFrom + " where " + sWhere; } return null; } public override bool bAddDisplayColumn(string sPrefixe, STRDisplayColumn oColumnName) { if (this.sqlPrefixe == sPrefixe) { oColumnName.sColumnName = sAddPrefixe(oColumnName.sColumnName, sPrefixe, '.'); oColumnName.bIsReflexive = false; m_oFieldNameArrayList.Add(oColumnName); return true; } else if (REFLEXIVE_PREFIXE + '_' + this.sqlPrefixe == sPrefixe) { oColumnName.sColumnName = sAddPrefixe(oColumnName.sColumnName, sPrefixe, '.'); oColumnName.bIsReflexive = true; m_oFieldNameArrayList.Add(oColumnName); return true; } return false; } public override string sGetTypeOf(string sFieldName) { for(int i=0; i < m_oFieldNameArrayList.Count; i++) { if (sFieldName == ((STRDisplayColumn)m_oFieldNameArrayList[i]).sColumnName) { return this.m_oTable. sGetTypeOf(((STRDisplayColumn)m_oFieldNameArrayList[i]).sColumnName); } } return null; } /* public override bool bAddDisplayDateColumn(string sPrefixe) { if (m_oTable.IsField(DATE_FIELD)) { STRDisplayColumn oDisplayColumn = new STRDisplayColumn(); oDisplayColumn.sColumnName = DATE_FIELD; oDisplayColumn.dColPos = m_dColPos; m_dColPos++; return bAddDisplayColumn(sPrefixe, oDisplayColumn); } return false; }*/ public override bool bAddWhere(string sPrefixe, string sColumnName, ENSQLRelation enRelation, string sValue) { // EPAssert(sqlPrefixe() == sPrefixe, sPrefixe + " is bot correct for table " + NAME_FIELD); if (sqlPrefixe == sPrefixe || REFLEXIVE_PREFIXE + '_' + sqlPrefixe == sPrefixe) { if (IsUsed(sValue)) { if (enRelation == ENSQLRelation.like) { m_oWhereArrayList.Add(sPrefixe + '.' + sColumnName + " like%" + m_oTable.FormatValue(sColumnName, sValue) + '%'); } /* else if (enRelation == ENSQLRelation.EqOrNotExist) { string sSqlNotExist = "select " + sPrefixe + "." + ENTITY_CLASS_INDEX_FIELD + this.sqlFrom + this.sqlGetIndexJoin + AND + GetSlaveTable.sFieldEqual(ENTITY_CLASS_INDEX_FIELD, (int)iValueArray.dClass); string sSql = "not exists(" + sSqlNotExist + ") or (" + m_oTable.sFieldEqual(ENTITY_CLASS_INDEX_FIELD, (int)iValueArray.dClass) + AND + GetSlaveTable.sFieldEqual(ENTITY_INDEX_FIELD, (int)iValueArray.iInstance); m_oWhereArrayList.Add(sPrefixe + '.' + sColumnName + " like%" + m_oTable.FormatValue(sColumnName, sValue) + '%'); }*/ else { m_oWhereArrayList.Add(sPrefixe + '.' + sColumnName + sGetRelation(enRelation) + m_oTable.FormatValue(sColumnName, sValue)); } } return true; } // CTrace.i().vWriteLn(ENMessage.DbS,"m_oWhereArrayList = " + m_oWhereArrayList.Count); return false; } public override bool bAddWhere(string sPrefixe1, string sColumnName1, ENSQLRelation enRelation, string sPrefixe2, string sColumnName2) { // EPAssert(sqlPrefixe() == sPrefixe, sPrefixe + " is bot correct for table " + NAME_FIELD); if (sqlPrefixe == sPrefixe1 || (REFLEXIVE_PREFIXE + '_' + sqlPrefixe == sPrefixe1) ) { m_oWhereArrayList.Add(sPrefixe1 + "." + sColumnName1 + sGetRelation(enRelation) + sPrefixe2 + "." + sColumnName2); } return true; } public override bool bAddWhere(string sPrefixe, string sSql) { if (this.sqlPrefixe == sPrefixe) { CTrace.i().vWriteLn(ENMessage.DbView, " sSql=" + sSql); m_oWhereArrayList.Add(sSql); return true; } return false; } /* public override bool bAddValueOrSetWhereCondition(string sPrefixe, string sColumn, string sValue) { string sSqlNotExist = GetSlaveTable.sFieldEqual(sColumn, sValue ); string sSqlExactSql = "(" + GetSlaveTable.sFieldEqual(ENTITY_CLASS_INDEX_FIELD, (int)iValueArray.dClass) + AND + GetSlaveTable.sFieldEqual(ENTITY_INDEX_FIELD, (int)iValueArray.iInstance); return bAddWhere(" not exists(" + "select " + sPrefixe + "." + sColumn + this.sqlFrom + " where " + GetSlaveTable.sFieldEqual(sColumn, sValue) + ") " + OR + sSqlExactSql); } public override bool bAddNotExistWhereCondition(string sPrefixe, string sColumn) { string sSqlNotExist = GetSlaveTable.sFieldEqual(ENTITY_CLASS_INDEX_FIELD, (int)iValueArray.dClass); string sSql = " not exists(" + sSqlNotExist + ") "; string sSqlExactSql = "(" + GetSlaveTable.sFieldEqual(ENTITY_CLASS_INDEX_FIELD, (int)iValueArray.dClass) + AND + GetSlaveTable.sFieldEqual(ENTITY_INDEX_FIELD, (int)iValueArray.iInstance); return bAddWhere(" not exists(" + "select " + sPrefixe + "." + ENTITY_CLASS_INDEX_FIELD + sSqlNotExist += this.sqlFrom + this.sqlGetIndexJoin(sPrefixe) + AND + sSqlNotExist + ") " + OR + sSqlExactSql); } public override bool bAddExistWhereCondition(string sPrefixe, string sColumn) { }*/ public override bool bAddOrderBy(string sPrefixe, string sColumnName) { // EPAssert(sqlPrefixe() == sPrefixe, sPrefixe + " is bot correct for table " + NAME_FIELD); if (sqlPrefixe == sPrefixe || REFLEXIVE_PREFIXE + '_' + sqlPrefixe == sPrefixe) { m_oOrderByArrayList.Add(sPrefixe + "." + sColumnName); return true; } // CTrace.i().vWriteLn(ENMessage.DbS,"m_oWhereArrayList = " + m_oWhereArrayList.Count); return false; } } ///---------------------------------------------------------------------------------------------------------------------------------------------- /// /// Summary description for DbMultiLanguageTable. /// ///---------------------------------------------------------------------------------------------------------------------------------------------- public abstract class CDbView : CDbConnectionBase { public CDbView(string sDatabaseName) { m_oWhereArrayList = new ArrayList(); m_oOrderByArrayList = new ArrayList(); m_dColPos = 0; } protected int m_dColPos; protected ArrayList m_oWhereArrayList, m_oOrderByArrayList; public abstract void vCleanAllData(); public bool bAddWhere(string sPrefixe, string sColumnName, int dValue) { return bAddWhere( sPrefixe, sColumnName, ENSQLRelation.eq, dValue); } public bool bAddWhereOrganisation(string sPrefixe, SIndex oOrganisationIndex) { bool bRet; bRet = bAddWhere(sPrefixe, ORGANISATION_INDEX_FIELD, oOrganisationIndex.dIndex); if (IsUsed(oOrganisationIndex)) { bRet = bAddWhere(sPrefixe, ORGANISATION_SUB_INDEX_FIELD, oOrganisationIndex.dSubIndex); } return bRet; } public bool bAddWhere(string sPrefixe, string sColumnName, ENSQLRelation enRelation, int dValue) { return bAddWhere( sPrefixe, sColumnName, enRelation, dValue.ToString() ); } /* public bool bAddWhere(string sPrefixe, string sColumnName, ENSQLRelation enRelation, CEntityArray oEntityArray) { bool bRet = true; for (int i = 0; i < oEntityArray.Length; i++) { if (IsUsed(oEntityArray[i])) { bRet = bAddWhere(sPrefixe, sColumnName, enRelation, oEntityArray[i].ToString()); } } return bRet; } */ public bool bAddWhere(string sPrefixe, string sColumnName, ENSQLRelation enRelation, P dValue) { if (IsUsed(dValue)) { return bAddWhere(sPrefixe, sColumnName, enRelation, ((int)dValue).ToString()); } return true; } public abstract bool bIsTableExist(); public bool bAddWhere(string sPrefixe, string sColumnName, string sValue) { return bAddWhere( sPrefixe, sColumnName, ENSQLRelation.eq, sValue); } public abstract bool bAddWhere(string sPrefixe, string sColumnName, ENSQLRelation enRelation, string sValue); public abstract bool bAddWhere(string sPrefixe1, string sColumnName1, ENSQLRelation enRelation, string sPrefixe2, string sColumnName2); public abstract bool bAddOrderBy(string sPrefixe, string sColumnName); public abstract string sqlOrderBy { get; } public abstract void vResetSelect(); public bool bAddWhere(string sPrefixe, string sColumnName, int[] dValueArray) { if (dValueArray.Length ==0) return true; bool bRet=false; for (int i = 0; i < dValueArray.Length; i++) { CTrace.i().vWriteLn(ENMessage.DbS,sPrefixe + "." + sColumnName + "=" + dValueArray[i]); EPAssert(dValueArray[i] != NOT_USED, " Wrong values"); bRet = bAddWhere(sPrefixe, sColumnName, ENSQLRelation.eq, dValueArray[i]); } return bRet; } public abstract string sqlSelect(string sPrefixe, string sColumn, string sWhere); public string sqlExist(string sPrefixe, string sColumn, string sWhere) { return " exists(" + sqlSelect(sPrefixe, sColumn, sWhere) + ") "; } public string sqlNotExist(string sPrefixe, string sColumn, string sWhere) { return " not exists(" + sqlSelect(sPrefixe, sColumn, sWhere) + ") "; } public abstract bool bAddWhere(string sPrefixe, string sWhere); /* public abstract bool bAddNotExistWhereCondition(string sPrefixe, string sColumn); public abstract bool bAddExistWhereCondition(string sPrefixe, string sColumn); public abstract bool bAddValueOrSetWhereCondition(string sPrefixe, string sColumn, string sValue); */ /* public bool bAddHeadWhere(string sPrefixe, CEntityArray oEntityArray) { bool bRet = bAddWhere(sPrefixe, LANGUAGE_FIELD, ENSQLRelation.eq, oEntityArray.Language); bRet = bAddWhere(sPrefixe, META_CLASS_FIELD, ENSQLRelation.eq, oEntityArray.MetaClass); bRet = bAddWhere(sPrefixe, CLASS_FIELD, ENSQLRelation.eq, oEntityArray.Class); bRet = bAddWhere(sPrefixe, NAME_FIELD, ENSQLRelation.eq, oEntityArray.Name); return bRet; }*/ public bool bAddWhere(string sPrefixe, SIndex oIndex) { return bAddWhere(sPrefixe, ENSQLRelation.eq, oIndex); } public bool bAddWhere(string sPrefixe, ENSQLRelation enRelation, SIndex oIndex) { bAddWhere(sPrefixe, MASTER_INDEX_FIELD, enRelation, oIndex.dIndex); return bAddWhere(sPrefixe, MASTER_SUB_INDEX_FIELD, enRelation, oIndex.dSubIndex); } public abstract string sqlWhere { get; } protected string sqlConcat(ArrayList oFieldNameArray, string sSeparation) { string sReturn = oFieldNameArray[0].ToString(); for (int i = 1; i < oFieldNameArray.Count; i++) { sReturn += sSeparation + oFieldNameArray[i].ToString(); } return sReturn; } protected virtual string sAddWhere() { string sWhere = sqlWhere; if (sqlGetIndexJoin(null) != null) { if (sWhere != null && sWhere.Length > 0) sWhere += AND; sWhere += sqlGetIndexJoin(null); } // CTrace.i().vWriteLn(ENMessage.DbS,"sAddWhere() sWhere = " + sWhere); return sWhere; } public virtual CDbView GetMasterView { get { return this; } } public struct STRDisplayColumn { public string sColumnName; public int dColPos; public string sColumnNameWithPrefixe; public bool bIsReflexive; } public bool bAddDisplayColumn(string sSuffix, string sColumnName) { STRDisplayColumn oDisplayColumn = new STRDisplayColumn(); oDisplayColumn.sColumnName = sColumnName; oDisplayColumn.dColPos = m_dColPos; m_dColPos++; return bAddDisplayColumn(sSuffix, oDisplayColumn); } public bool bAddDisplayReflexiveColumn(string sSuffix, string sColumnName) { return bAddDisplayColumn(REFLEXIVE_PREFIXE + '_' + sSuffix, sColumnName); } public abstract string sGetTypeOf(string sFieldName); public abstract bool bAddDisplayColumn(string sSuffix, STRDisplayColumn sColumnName); public abstract string sqlFrom { get; } public abstract string sGetMasterTableName { get; } public virtual string sqlGetIndexJoin(string sPrefixe) { return null; } public abstract string sqlReflexiveFrom { get; } public abstract int dGetDisplayFieldCount { get; } public abstract STRDisplayColumn GetFieldName(int i); public string sGetFieldNameFromPosition(int dCol) { for (int i = 0; i < dGetDisplayFieldCount; i++) { if (GetFieldName(i).dColPos == dCol) { return GetFieldName(i).sColumnName; } } return null; } public string sGetFieldList { get { if (dGetDisplayFieldCount <= 0) return null; string sFieldList = sGetFieldNameFromPosition(0); for (int i = 1; i < dGetDisplayFieldCount; i++) { sFieldList += ", " + sGetFieldNameFromPosition(i); } return sFieldList; } } public virtual string sql(string sFieldList) { string sSelect = "SELECT " + sFieldList + " FROM " + sqlFrom; string sWhere = sAddWhere(); if (sWhere != null) { sSelect += " where " + sWhere; } string sOrderBy = sqlOrderBy; if (sOrderBy != null) { sSelect += " order by " + sOrderBy; } return sSelect; } public string sql() { return sql(sGetFieldList); } public string sqlCount() { return sql("count(*)"); } public ec dGetSelect(out ArrayList oOutputArray) { // CTrace.i().vWriteLn(ENMessage.DbS,"sql(sGetFieldList) = " + sql(sGetFieldList)); return dGetSelect(sql(sGetFieldList), out oOutputArray); } public ec dGetSelect(int dMaxRecord, out ArrayList oOutputArray) { // CTrace.i().vWriteLn(ENMessage.DbS,"sql(sGetFieldList) = " + sql(sGetFieldList)); if (IsUsed(dMaxRecord)) { return dGetSelect(sql(" top " + dMaxRecord + ' ' + sGetFieldList), out oOutputArray); } else { return dGetSelect(sql(sGetFieldList), out oOutputArray); } } protected ec dGetSelect( string sql, out ArrayList oOutputArray) { CDbConnection oConnection = GetConnection(); oConnection.Open(); CTrace.i().vWriteLn(ENMessage.DbS,sql); if (oConnection.SelectCommand(sql) == false) { oOutputArray = null; return ec.ST_DATABASE_SELECTION_ERROR; } oOutputArray = new ArrayList(); while (oConnection.Read()) { object[] oValueArray = new object[dGetDisplayFieldCount]; oConnection.Reader.GetValues(oValueArray); oOutputArray.Add(oValueArray); } oConnection.Close(); if (oOutputArray.Count == 0) { oOutputArray = null; return ec.ST_SQL_THE_RETURN_ROW_SET_IS_EMPTY; } return ec.ST_SUCCEED; } protected object[] ReplaceWithType(string[] sFieldName, object[] oObjs) { object[] oObjectArray = new object[oObjs.Length]; for (int i = 0; i < oObjs.Length; i++) { if (this.sGetTypeOf(sFieldName[i]) == ENTITY_TYPE) { oObjectArray[i] = (P)oObjs[i]; } else { oObjectArray[i] = oObjs[i]; } } return oObjectArray; } public ec dGetRecordCount(string sql, out int dCount) { dCount = 0; CDbConnection oConnection = GetConnection(); oConnection.Open(); if (oConnection.SelectCommand(sql) == false) { return ec.ST_DATABASE_SELECTION_ERROR; } CTrace.i().vWriteLn(ENMessage.DbS,sql); while (oConnection.Read()) { dCount = oConnection.Reader.GetInt32(0); } oConnection.Close(); return ec.ST_SUCCEED; } public string csDumpTable() { return csDump(sql("*")); } public string csDump(string sql) { CDbConnection oConnection = GetConnection(); oConnection.Open(); CTrace.i().vWriteLn(ENMessage.DbS,"sql() = " + sql); oConnection.SelectCommand(sql); object[] oValues = new object[dGetDisplayFieldCount]; StringBuilder oReport = new StringBuilder(); while (oConnection.Read()) { int dCount = oConnection.Reader.GetValues(oValues); for (int i = 0; i < dCount; i++) { oReport.Append(oValues[i]); oReport.Append(','); } oReport.Append('\n'); } oConnection.Close(); return oReport.ToString(); } } ///---------------------------------------------------------------------------------------------------------------------------------------------- /// /// Summary description for DbMultiLanguageTable. /// ///---------------------------------------------------------------------------------------------------------------------------------------------- public abstract class CDbNestedSubQueryView : CDb2JoinView { public CDbNestedSubQueryView(CDbView oMasterView, CDbView oNestedView) : base(oMasterView, oNestedView) { m_sqlMasterColumnSubQueryLink = m_sqlSlaveColumnSubQueryLink = null; } public override string sql(string sFieldList) { CTrace.i().vWriteLn(ENMessage.DbS,"Slave View = " + m_oSlaveView.sql(m_sqlSlaveColumnSubQueryLink)); string sSelect = "SELECT " + sFieldList + " FROM " + sqlFrom + " where "; sSelect += " (" + m_sqlMasterColumnSubQueryLink + ") in ( " + m_oSlaveView.sql(m_sqlSlaveColumnSubQueryLink) + ") "; CTrace.i().vWriteLn(ENMessage.DbS,"sSelect = " + sSelect); if (sAddWhere() != null) { sSelect += AND; sSelect += sAddWhere(); } CTrace.i().vWriteLn(ENMessage.DbS,"sSelect = " + sSelect); return sSelect; } public override string sqlFrom { get { return m_oMasterView.sqlFrom; } } string m_sqlMasterColumnSubQueryLink, m_sqlSlaveColumnSubQueryLink; public override void vAddLinkIndex(string sMasterPrefixe, string sMasterFieldName, string sSlavePrefixe, string sSlaveFieldName) { if (m_sqlMasterColumnSubQueryLink != null) { m_sqlMasterColumnSubQueryLink += ", "; } m_sqlMasterColumnSubQueryLink += sMasterPrefixe + "." + sMasterFieldName; if (m_sqlSlaveColumnSubQueryLink != null) { m_sqlSlaveColumnSubQueryLink += ", "; } m_sqlSlaveColumnSubQueryLink += sSlavePrefixe + "." + sSlaveFieldName; } public override bool bAddWhere(string sPrefixe, string sColumnName, ENSQLRelation enRelation, string sValue) { if (m_oMasterView.bAddWhere(sPrefixe, sColumnName, enRelation, sValue)) { return true; } return false; } public override string sqlWhere { get { string sWhere = null; if (m_oWhereArrayList.Count != 0) { sWhere += sqlConcat(m_oWhereArrayList, AND); } if (m_oMasterView.sqlWhere != null) { if (sWhere != null) sWhere += AND; sWhere += m_oMasterView.sqlWhere; } return sWhere; } } public override string sqlOrderBy { get { string sOrderBy = null; if (m_oOrderByArrayList.Count != 0) { sOrderBy += sqlConcat(m_oOrderByArrayList, ","); } if (m_oMasterView.sqlOrderBy != null) { if (sOrderBy != null) sOrderBy += ", "; sOrderBy += m_oMasterView.sqlOrderBy; } return sOrderBy; } } public override bool bAddDisplayColumn(string sSuffix, STRDisplayColumn sColumnName) { if (m_oMasterView.bAddDisplayColumn(sSuffix, sColumnName)) { return true; } else if (m_oSlaveView.bAddDisplayColumn(sSuffix, sColumnName)) { return true; } return false; } public override int dGetDisplayFieldCount { get { return m_oMasterView.dGetDisplayFieldCount; } } public override STRDisplayColumn GetFieldName(int i) { if (i >= 0 && i < m_oMasterView.dGetDisplayFieldCount) { return m_oMasterView.GetFieldName(i); } else { return new STRDisplayColumn(); } } public override string sqlGetIndexJoin(string sPrefixe) { string sqlJoin = null; if (m_oMasterView.sqlGetIndexJoin(sPrefixe) != null) { if (sqlJoin != null) sqlJoin += AND; sqlJoin += m_oMasterView.sqlGetIndexJoin(sPrefixe); } return sqlJoin; } } ///---------------------------------------------------------------------------------------------------------------------------------------------- /// /// Summary description for DbMultiLanguageTable. /// ///---------------------------------------------------------------------------------------------------------------------------------------------- public class CDbReflexiveTable : CDbSTable { public CDbReflexiveTable(string strTableName, string strDatabaseName) : base(strTableName, strDatabaseName) { } public override void vAddColumn(string strFieldName, string strType, bool bPrimaryKey) { base.vAddColumn(strFieldName, strType, bPrimaryKey); base.vAddColumn(REFLEXIVE + strFieldName, strType, bPrimaryKey); } public const string REFLEXIVE = "R"; public override ec dGetSelect(string strWhere, out ArrayList oOutputArray, params string[] sFieldNameArray) { ArrayList oOutputArray1, oOutputArray2; ec dRC1 = base.dGetSelect(strWhere, out oOutputArray1, sFieldNameArray); ec dRC2 = base.dGetSelect(sAddPrefixe(strWhere, REFLEXIVE, '_'), out oOutputArray2, sAddPrefixe(sFieldNameArray, REFLEXIVE, '_')); oOutputArray = new ArrayList(oOutputArray1.Count + oOutputArray2.Count); oOutputArray.AddRange(oOutputArray1); oOutputArray.AddRange(oOutputArray2); return dRC1 == ec.ST_SUCCEED ? dRC2 : dRC1; } public override ec dGetRecordCount(string sWhere, out int dCount) { ec dRet = ec.ST_SUCCEED; int dCount1; dRet = base.dGetRecordCount(sWhere, out dCount1); if (dCount1 <= 0) { dCount = dCount1; } else { int dCount2; dRet= base.dGetRecordCount(sAddPrefixe(sWhere, REFLEXIVE, '_'), out dCount2); if (dCount2 <= 0) { dCount = dCount2; } else { dCount = dCount1 + dCount2; } } return dRet; } public override object GetValue(string strColumnValue, string strWhere) { if (!IsTableExist) return null; object oReturn = base.GetValue(strColumnValue, strWhere); if (oReturn == null || oReturn.ToString().Length == 0) { oReturn = base.GetValue(strColumnValue, sAddPrefixe(strWhere, REFLEXIVE, '_')); } return oReturn; } public void vAddValue(string sValue1, string sValue2) { base.vAddValue(sValue1); base.vAddValue(sValue2); } public void vAddValue(int dValue1, int dValue2) { base.vAddValue(dValue1); base.vAddValue(dValue2); } public override ec dDelete(string sWhere) { return dDelete(sWhere + " or " + sAddPrefixe(sWhere, REFLEXIVE, '_')); } } ///---------------------------------------------------------------------------------------------------------------------------------------------- /// /// Summary description for DbMultiLanguageTable. /// ///---------------------------------------------------------------------------------------------------------------------------------------------- public class CDbSTable : CDbTable { public CDbSTable(string strTableName, string strDatabaseName) : base(strTableName) { m_strDatabaseName = strDatabaseName; m_oFieldMap = new ArrayList(); } // ENBoolean m_ebIsTableExist string m_strDatabaseName; // protected virtual void vAddFirstColumns() { } public override string sDatabaseName { get { return m_strDatabaseName; } } public override bool SetName(string strTableName) { Clear(); m_strTableName = ConvertToTableName(strTableName); // CTrace.i().vWriteLn(ENMessage.DbS,"SetName = " + strTableName + " " + m_bIsTableExist); m_strWhereKey = null; m_bValid = true; m_ebIsTableExist = ENBoolean.Unknown; return true; } public void vAddAutoIndexColumn(string strIndexColumn, string strNameColumn) { m_strIndexColumn = strIndexColumn; m_strNameColumn = strNameColumn; vAddColumn(strIndexColumn, "Auto", true); vAddColumn(strNameColumn, "String 50", false); } /* public string sFieldEqual(string strField, string strValue) { return strField + " = " + FormatValue(strField, strValue); } */ public int dFindIndex(string strName) { return this.GetInteger(m_strIndexColumn, this.sFieldEqual(m_strNameColumn, strName)); } public override ec dCreateIndex() { ec dRC = base.dCreatePrimaryIndex(); if (m_strNameColumn != null) { dRC=dCreateIndex(2, m_strNameColumn); } return dRC; } public ec dResetDate(string sWhere) { int dDateIndex = IndexOf(DATE_FIELD); if (dDateIndex >= 0) { return dUpdate(DATE_FIELD, DateTime.UtcNow.ToString(), sWhere); } return ec.ST_SQL_NO_DATE_FIELD; } string m_strIndexColumn, m_strNameColumn; public int dGetNext(string sColumnName, string sWhere) { int dNewValue = dGetMax(sColumnName, sWhere); if (IsNotUsed(dNewValue)) { dNewValue = 1; } else { dNewValue++; } return dNewValue; } public bool bGetSelect(string strFieldName1, string strFieldName2, string strWhere, out int nValue1, out int nValue2) { CDbConnection oConnection = GetConnection(); oConnection.Open(); nValue1 = NOT_USED; nValue2 = NOT_USED; string strTableNameSelect = "SELECT " + strFieldName1 + "," + strFieldName2 + " FROM " + m_strTableName; if (strWhere != null && strWhere.Length > 0) { strTableNameSelect += " where " + strWhere; } CDbConnectionManagement.Instance().ResetRetryCounter(); if (oConnection.SelectCommand(strTableNameSelect) == false) { return false; } CTrace.i().vWriteLn(ENMessage.DbS,strTableNameSelect); if (oConnection.Read() == false) { CTrace.i().vWriteLn(ENMessage.sqlError, "\n###1There are nothing to read for the table symbol for the condition " + strWhere + "###\n"); } else { nValue1 = oConnection.Reader.GetInt32(0); nValue2 = oConnection.Reader.GetInt32(1); } oConnection.Close(); return true; } public string sFieldContains(string strField, CEntityArray oEntityArray) { string sqlResult = null; for (int i = 0; i < oEntityArray.Length; i++) { if (i >= 1) sqlResult += AND; sqlResult += sFieldContains(strField, oEntityArray[i]); } return sqlResult; } public string sFieldContains(string strField, SEntity oEntity) { return strField + " like %" + FormatValue(strField, oEntity.ToString() ) + '%'; } public bool bGetSelect(string strFieldName1, string strFieldName2, string strFieldName3, string strWhere, out int dValue1, out int dValue2, out bool bValue3) { CDbConnection oConnection = GetConnection(); oConnection.Open(); dValue2 = dValue1 = NOT_USED; bValue3 = false; string strTableNameSelect = "SELECT " + strFieldName1 + "," + strFieldName2 + "," + strFieldName3 + " FROM " + m_strTableName; if (strWhere != null && strWhere.Length > 0) { strTableNameSelect += " where " + strWhere; } if (oConnection.SelectCommand(strTableNameSelect) == false) { return false; } CTrace.i().vWriteLn(ENMessage.DbS,strTableNameSelect); if (oConnection.Read() == false) { CTrace.i().vWriteLn(ENMessage.sqlError, "\n###2There are nothing to read for the table symbol for the condition " + strWhere + "###\n"); } else { dValue1 = oConnection.Reader.GetInt32(0); dValue2 = oConnection.Reader.GetInt32(1); bValue3 = oConnection.Reader.GetBoolean(2); } oConnection.Close(); return true; } public bool bGetSelect(string strFieldName1, string strFieldName2, string strWhere, out int nValue1, out string strValue2) { CDbConnection oConnection = GetConnection(); oConnection.Open(); nValue1 = NOT_USED; strValue2 = null; string strTableNameSelect = "SELECT " + strFieldName1 + "," + strFieldName2 + " FROM " + m_strTableName; if (strWhere != null && strWhere.Length > 0) { strTableNameSelect += " where " + strWhere; } CDbConnectionManagement.Instance().ResetRetryCounter(); if (oConnection.SelectCommand(strTableNameSelect) == false) { return false; } CTrace.i().vWriteLn(ENMessage.DbS,strTableNameSelect); if (oConnection.Read() == false) { CTrace.i().vWriteLn(ENMessage.sqlError, "\n###3There are nothing to read for the table symbol for the condition " + strWhere + "###\n"); } else { nValue1 = oConnection.Reader.GetInt32(0); strValue2 = oConnection.Reader.GetString(1); } oConnection.Close(); return true; } public virtual ec dGetSelect(string strWhere, out ArrayList oOutputArray, params string[] strFieldNameArray) { if (IsTableExist == false) { oOutputArray = null; CTrace.i().vWriteLn(ENMessage.sqlWarning, this.m_strTableName + " does not exists"); return ec.ST_SQL_TABLE_NAME_UNKNOWN; } EPAssert(strFieldNameArray.Length >= 1, "should not be null"); if (strFieldNameArray.Length <= 0) { oOutputArray = null; return ec.ST_BUG_INPUT_ARRAY_EMPTY; } string sFieldNameList = strFieldNameArray[0]; for (int i = 1; i < strFieldNameArray.Length; i++) { sFieldNameList += ", " + strFieldNameArray[i]; } CDbConnection oConnection = GetConnection(); oConnection.Open(); oOutputArray = new ArrayList(); string strTableNameSelect = "SELECT " + sFieldNameList + " FROM " + m_strTableName; if (strWhere != null && strWhere.Length > 0) { strTableNameSelect += " where " + strWhere; } CDbConnectionManagement.Instance().ResetRetryCounter(); if (oConnection.SelectCommand(strTableNameSelect) == false) { return ec.ST_DATABASE_SELECTION_ERROR; } CTrace.i().vWriteLn(ENMessage.sqlTrace, "SQL=" +strTableNameSelect); while (oConnection.Read()) { object[] oValueArray = new object[strFieldNameArray.Length]; oConnection.Reader.GetValues(oValueArray); oOutputArray.Add(this.ReplaceWithType(strFieldNameArray, oValueArray)); CTrace.i().vWriteLn(ENMessage.sqlTrace, "Read=" + FromObjectToString(this.ReplaceWithType(strFieldNameArray, oValueArray)) ); } oConnection.Close(); if (oOutputArray.Count == 0) { oOutputArray = null; } return ec.ST_SUCCEED; } public virtual ec dGetRecordCount(string sWhere, out int nCount) { nCount = 0; if (IsTableExist == false) return ec.ST_SQL_TABLE_NAME_UNKNOWN; CDbConnection oConnection = GetConnection(); string strTableNameSelect = "SELECT COUNT(*) FROM " + m_strTableName; CTrace.i().vWriteLn(ENMessage.sqlTrace, "SQL = " + strTableNameSelect + "\n"); if (sWhere != null) { strTableNameSelect += " where " + sWhere; } oConnection.Open(); oConnection.SelectCommand(strTableNameSelect); while (oConnection.Read()) { nCount = oConnection.Reader.GetInt32(0); } oConnection.Close(); return ec.ST_SUCCEED; } public ec bGetSelect(string strFieldName1, string strFieldName2, string strFieldName3, string strDateField, string strWhere, out int nValue1, out int nValue2, out int nValue3, out DateTime oDate) { CDbConnection oConnection = GetConnection(); oConnection.Open(); nValue1 = nValue2 = nValue3 = NOT_USED; oDate = NOT_USED_DATE; string strTableNameSelect = "SELECT " + strFieldName1 + "," + strFieldName2 + "," + strFieldName3 + "," + strDateField + " FROM " + m_strTableName; if (strWhere != null && strWhere.Length > 0) { strTableNameSelect += " where " + strWhere; } CDbConnectionManagement.Instance().ResetRetryCounter(); if (oConnection.SelectCommand(strTableNameSelect) == false) { return ec.ST_SQL_SELECT_FAILED; } CTrace.i().vWriteLn(ENMessage.DbS,strTableNameSelect); if (oConnection.Read() == false) { CTrace.i().vWriteLn(ENMessage.sqlError, "\n###4There are nothing to read for the table symbol for the condition " + strWhere + "###\n"); } else { nValue1 = oConnection.Reader.GetInt32(0); nValue2 = oConnection.Reader.GetInt32(1); nValue3 = oConnection.Reader.GetInt32(2); oDate = oConnection.Reader.GetDateTime(3); } oConnection.Close(); return ec.ST_SUCCEED; } public int dGetIntegerSum(string strFieldName1, string strFieldName2, string strWhere) { int nValue1 = 0, nValue2 = 0; if (bGetSelect(strFieldName1, strFieldName2, strWhere, out nValue1, out nValue2)) { return nValue1 + nValue2; } else { return NOT_USED; } } /* public bool bRollClickUp(string strNumberFieldName, string strPreviousNumberFieldName, string strNumberArchiveFieldName, string strDateField, TimeSpan oTimeSpan, int nNameIndex) { int nClick, nPreviousClick, nArchiveClick; DateTime oLastUpdatedDate; if (bGetSelect(strNumberFieldName, strPreviousNumberFieldName, strNumberArchiveFieldName, strDateField, sFieldEqual(NAME_INDEX_FIELD, nNameIndex), out nClick, out nPreviousClick, out nArchiveClick, out oLastUpdatedDate)) { if ((oLastUpdatedDate + oTimeSpan) > DateTime.UtcNow) { return false; } if (nClick != NOT_USED && nArchiveClick != NOT_USED) { nArchiveClick += nPreviousClick; nPreviousClick = nClick; nClick = 0; return this.Update(strNumberFieldName, strPreviousNumberFieldName, strNumberArchiveFieldName, strDateField, "", "0", nPreviousClick.ToString(), nArchiveClick.ToString(), DateTime.UtcNow.ToString(), "", sFieldEqual(NAME_INDEX_FIELD, nNameIndex)); } } return false; }*/ public void vAddFilter(CCommandFactoryBase.P dEntityClassFilter, string sFieldName) { vAddColumn(sFieldName, INTEGER_TYPE, false); SEntityToField oEntityToField; oEntityToField.dEntityClass = dEntityClassFilter; oEntityToField.dFieldIndex = FieldCount - 1; } public struct SEntityToField { public CCommandFactoryBase.P dEntityClass; public int dFieldIndex; } ArrayList m_oFieldMap; public int dGetFieldIndex(CCommandFactoryBase.P dEntityClass) { for(int i=0; i < m_oFieldMap.Count; i++) { if ( ((SEntityToField)m_oFieldMap[i]).dEntityClass == dEntityClass) { return ((SEntityToField)m_oFieldMap[i]).dFieldIndex; } } return NOT_USED; } public string sFieldEqual(CCommandFactoryBase.P dEntityClass, CCommandFactoryBase.P dEntity) { int dFieldIndex = dGetFieldIndex(dEntityClass); if (IsUsed(dFieldIndex)) { return this.sFieldEqual(this.m_oFieldNameList[dFieldIndex].ToString(), dEntity); } return null; } public string sFieldEqual(string strField, CCommandFactoryBase.P dEntity) { return strField + " = " + FormatValue(strField, ((int)dEntity).ToString() ); } public string sFieldEqual(string strField, char cChar) { return strField + " = " + FormatValue(strField, cChar.ToString()); } public string FieldName(CCommandFactoryBase.P dEntityClass) { int dFieldIndex = dGetFieldIndex(dEntityClass); if (IsUsed(dFieldIndex)) { return (string)m_oFieldNameList[dFieldIndex]; } return null; } public void vAddValue(SIndex oIndex) { vAddValue(oIndex.dIndex); vAddValue(oIndex.dSubIndex); } public void vAddValue(SEntity oEntity) { vAddValue(oEntity.dClass); vAddValue(oEntity.sql); } /* public string sql(CEntityArray oEntityArray) { string sSql = sFieldEqual(META_CLASS_FIELD, oEntityArray.MetaClass); if (IsUsed(oEntityArray.Class)) { sSql += AND + sFieldEqual(CLASS_FIELD, oEntityArray.Class); } if (IsUsed(oEntityArray.Name)) { sSql += AND + sFieldEqual(NAME_FIELD, oEntityArray.Name); } if (IsUsed(oEntityArray.Language)) { sSql += AND + sFieldEqual(LANGUAGE_FIELD, oEntityArray.Language); } return sSql; } */ public object Retrieve(string strKey) { string strSqlWhere = strKey; byte[] binValue = (byte[])GetValue(VALUE_FIELD, strSqlWhere); if (binValue == null) { return null; } else { return Deserialize(binValue); } } public string sqlIsOrganisation(SIndex oOrganisationIndex) { if (IsUsed(oOrganisationIndex.dSubIndex)) { return sFieldEqual(ORGANISATION_INDEX_FIELD, oOrganisationIndex.dIndex) + AND + sFieldEqual(ORGANISATION_SUB_INDEX_FIELD, oOrganisationIndex.dSubIndex); } else { return sFieldEqual(ORGANISATION_INDEX_FIELD, oOrganisationIndex.dIndex); } } public string sqlIsMeme(SIndex oIndex) { if (IsUsed(oIndex.dSubIndex)) { return sFieldEqual(MEME_INDEX_FIELD, oIndex.dIndex) + AND + sFieldEqual(MEME_SUB_INDEX_FIELD, oIndex.dSubIndex); } else { return sFieldEqual(MEME_INDEX_FIELD, oIndex.dIndex); } } public string sqlIsProfile(SIndex oIndex) { if (IsUsed(oIndex.dSubIndex)) { return sFieldEqual(PROFILE_INDEX_FIELD, oIndex.dIndex) + AND + sFieldEqual(PROFILE_SUB_INDEX_FIELD, oIndex.dSubIndex); } else { return sFieldEqual(PROFILE_INDEX_FIELD, oIndex.dIndex); } } public string sqlIs(SIndex oIndex) { if (IsUsed(oIndex.dSubIndex)) { return sFieldEqual(MASTER_INDEX_FIELD, oIndex.dIndex) + AND + sFieldEqual(MASTER_SUB_INDEX_FIELD, oIndex.dSubIndex); } else { return sFieldEqual(MASTER_INDEX_FIELD, oIndex.dIndex); } } } ///---------------------------------------------------------------------------------------------------------------------------------------------- /// /// Summary description for DbMultiLanguageTable. /// ///---------------------------------------------------------------------------------------------------------------------------------------------- /* public interface IReferenceTable: CDbEPBase { void vAddReference(SIndex oContaningDocument, SIndex oReferenceDocument, int dReferenceVersion); void vRemoveReference(SIndex oContaningDocument, SIndex oReferenceDocument); void vUpdateReference(SIndex oContaningDocument, SIndex oReferenceDocument, int dReferenceVersion); }*/ public class CDbMemeArchiveTable : CDbArchiveTable { public CDbMemeArchiveTable(string strTableName, string strDatabaseName) : base(strTableName, strDatabaseName) { m_oXRefTable = new CDbSTable(strTableName+VERSION_XREF, sDatabaseName); m_oXRefTable.vAddColumn(MASTER_INDEX_FIELD, INTEGER_TYPE, true); m_oXRefTable.vAddColumn(MASTER_SUB_INDEX_FIELD, INTEGER_TYPE, true); m_oXRefTable.vAddColumn(VERSION_FIELD, INTEGER_TYPE, true); m_oXRefTable.vAddColumn(REF_INDEX_FIELD, INTEGER_TYPE, false); m_oXRefTable.vAddColumn(REF_SUB_INDEX_FIELD, INTEGER_TYPE, false); if (m_oXRefTable.dCreateTable() == ec.ST_SUCCEED) { m_oXRefTable.dCreateIndexMultipleField(2, REF_INDEX_FIELD, REF_SUB_INDEX_FIELD); } } CDbSTable m_oXRefTable; public const string VERSION_XREF = ".xref", REF_INDEX_FIELD = "[xref_index_field]", REF_SUB_INDEX_FIELD = "[xref_sub_index_field]"; public string sqlIsIndex(SIndex oIndex) { return sFieldEqual(MASTER_INDEX_FIELD, oIndex.dIndex) + AND + sFieldEqual(MASTER_SUB_INDEX_FIELD, oIndex.dSubIndex); } public string sqlIsIndex(SIndex oIndex, int dVersion) { if (IsUsed(dVersion)) { return m_oXRefTable.sFieldEqual(MASTER_INDEX_FIELD, oIndex.dIndex) + AND + m_oXRefTable.sFieldEqual(MASTER_SUB_INDEX_FIELD, oIndex.dSubIndex) + AND + m_oXRefTable.sFieldEqual(VERSION_FIELD, dVersion); } else { return m_oXRefTable.sFieldEqual(MASTER_INDEX_FIELD, oIndex.dIndex) + AND + m_oXRefTable.sFieldEqual(MASTER_SUB_INDEX_FIELD, oIndex.dSubIndex); } } public string sqlIsXRefIndex(SIndex oIndex) { return m_oXRefTable.sFieldEqual(REF_INDEX_FIELD, oIndex.dIndex) + AND + m_oXRefTable.sFieldEqual(REF_SUB_INDEX_FIELD, oIndex.dSubIndex); } public ec dGetSelectLastVersion(SIndex oIndex, out ArrayList oOutputArray, params string[] strFieldNameArray) { return base.dGetSelectLastVersion(sqlIsIndex(oIndex), out oOutputArray, strFieldNameArray); } public ec dDeleteOldVersion(DateTime oLastVersion, SIndex oIndex) { return base.dDeleteOldVersion(oLastVersion, sqlIsIndex(oIndex)); } public ec dGetSelectLastVersion(int dNumberOfVersion, SIndex oIndex, out ArrayList oOutputArray, params string[] strFieldNameArray) { return base.dGetSelectLastVersion(dNumberOfVersion, sqlIsIndex(oIndex), out oOutputArray, strFieldNameArray); } public ec dAddReference(SIndex oContainingDocument, SIndex oReferenceDocument, int dReferenceVersion) { m_oXRefTable.vAddValue(oReferenceDocument); m_oXRefTable.vAddValue(dReferenceVersion); m_oXRefTable.vAddValue(oContainingDocument); return m_oXRefTable.dInsertIntoTable(); } public ec dRemoveAllReferences(SIndex oContainingDocument) { return m_oXRefTable.dDelete(sqlIsXRefIndex(oContainingDocument)); } public ec dRemoveReference(SIndex oContainingDocument, SIndex oReferenceDocument) { return m_oXRefTable.dDelete(sqlIsIndex(oReferenceDocument, NOT_USED) + AND + sqlIsXRefIndex(oContainingDocument)); } public ec dUpdateReference(SIndex oContainingDocument, SIndex oReferenceDocument, int dReferenceVersion) { return m_oXRefTable.dUpdate(VERSION_FIELD, dReferenceVersion.ToString(), sqlIsIndex(oReferenceDocument, NOT_USED) + AND + sqlIsXRefIndex(oContainingDocument)); } } ///---------------------------------------------------------------------------------------------------------------------------------------------- /// /// Summary description for DbMultiLanguageTable. /// ///---------------------------------------------------------------------------------------------------------------------------------------------- public class CDbArchiveTable : CDbSTable { public CDbArchiveTable(string strTableName, string strDatabaseName) : base(strTableName, strDatabaseName) { m_oArchiveTable = new CDbSTable(m_strTableName + ARCHIVE, sDatabaseName); m_oArchiveTable.vAddColumn(USED_DATE_FIELD, DATE_TYPE, true); m_oArchiveTable.vAddColumn(VERSION_FIELD, INTEGER_TYPE, true); } CDbSTable m_oArchiveTable; const string ARCHIVE = ".arch"; public override void vAddColumn(string strFieldName, string strType, bool bPrimaryKey) { base.vAddColumn(strFieldName, strType, bPrimaryKey); m_oArchiveTable.vAddColumn(strFieldName, strType, bPrimaryKey); } public override ec dCreateIndex() { m_oArchiveTable.dCreateIndex(); return base.dCreateIndex(); } public int dGetCurrentVersion(string strWhere) { return GetInteger(VERSION_FIELD, strWhere); } public ec dGetSelectLastVersion(string strWhere, out ArrayList oOutputArray, params string[] strFieldNameArray) { string sWhere = strWhere; if (sWhere != null && sWhere.Length > 0) { sWhere += AND; } sWhere = VERSION_FIELD + "= max(" + VERSION_FIELD + ")"; return m_oArchiveTable.dGetSelect(sWhere, out oOutputArray, strFieldNameArray); } public ec dGetSelectLastVersion(int dNumberOfVersion, string strWhere, out ArrayList oOutputArray, params string[] strFieldNameArray) { int dVersion = dGetCurrentVersion(strWhere); if (IsNotUsed(dVersion)) { oOutputArray = null; return ec.ST_ATTRIBUTE_CAN_NOT_FIND_THE_VERSION; } dVersion -= dNumberOfVersion; if (dVersion < 0) dVersion = 0; string sWhere = strWhere; if (sWhere != null && sWhere.Length > 0) { sWhere += AND; } sWhere = VERSION_FIELD + ">=" + m_oArchiveTable.FormatDateValue(dVersion.ToString()); return m_oArchiveTable.dGetSelect(sWhere, out oOutputArray, strFieldNameArray); } public ec dDeleteOldVersion(DateTime oLastVersion, string strWhere) { string sWhere = strWhere; if (sWhere != null && sWhere.Length > 0) { sWhere += AND; } sWhere = USED_DATE_FIELD + "<=" + m_oArchiveTable.FormatDateValue(oLastVersion.ToString()); return m_oArchiveTable.dDelete(sWhere); } public override ec dDelete(string sWhere) { base.dDelete(sWhere); return m_oArchiveTable.dDelete(sWhere); } public override ec dUpdate(string strField, string strValue, string strWhere) { ec dRC = ec.ST_SUCCEED; bool bRet = base.StartSelection(); if (bRet) { CDbRecordArray oArray; bRet = base.NextRecord(out oArray); if (oArray.bIsEqual(strField, strValue)) { dRC = ec.ST_ATTRIBUTE_CAN_NOT_UPDATE_THE_ATTRIBUTE_WITH_SAME_VALUE; } else { m_oArchiveTable.vAddValue(DateTime.UtcNow); m_oArchiveTable.vAddInsertValuesFrom(oArray.sGetAllField()); m_oArchiveTable.dInsertIntoTable(); oArray.bUpdate(strField, strValue); base.vAddInsertValuesFrom(oArray.sGetAllField()); dRC = base.dInsertIntoTable(); } } return dRC; } public ec dGetSelectAllVersion(string strWhere, out ArrayList oOutputArray, params string[] strFieldNameArray) { return m_oArchiveTable.dGetSelect(strWhere, out oOutputArray, strFieldNameArray); } public override ec dInsertIntoTable() { string sInsertedValues = base.sInsertedValues; CTrace.i().vWriteLn(ENMessage.DbS,"sInsertedValues = " + sInsertedValues); ec dRC = base.dInsertIntoTable(); if (dRC == ec.ST_SQL_INSERT_RECORD_ALREADY_EXIST) { m_oArchiveTable.vAddValue(DateTime.UtcNow); m_oArchiveTable.vAddInsertValuesFrom(sInsertedValues); dRC = m_oArchiveTable.dInsertIntoTable(); base.dDeleteCurrent(); dRC = base.dInsertIntoTable(); } return dRC; } public override ec dCreateTable() { m_oArchiveTable.dCreateTable(); return base.dCreateTable(); } } }

Enter supporting content here