using KGIS.Plugin.LayerProperty.Enum; using KGIS.Plugin.LayerProperty.Interface; using KGIS.Plugin.LayerProperty.Utils.Storage; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Net.NetworkInformation; using System.Text; using System.Threading.Tasks; using System.Xml.Serialization; namespace KGIS.Plugin.LayerProperty.Utils { public abstract class DataSourceGeneric { private const string TBSYS_DATASOURCE = "TBSYS_DATASOURCE"; private const string F_ID = "F_ID"; private const string F_NAME = "F_NAME"; private const string F_KEY = "F_KEY"; private const string F_ATTRIBUTE = "F_ATTRIBUTE"; private const string F_TYPE = "F_TYPE"; private const string F_DSTYPE = "F_DSTYPE"; private EnumDataSource dsType; private DatabaseType rdsType; private string mark = ""; private string name = ""; private string user = ""; private string password = ""; [XmlAttribute] public EnumDataSource DSType { get { return this.dsType; } protected set { this.dsType = value; } } [XmlIgnore] public DatabaseType RDSType { get { return this.rdsType; } set { this.rdsType = value; } } [XmlAttribute] public string Mark { get { return this.mark; } set { this.mark = value; } } [XmlAttribute] public string Name { get { return this.name; } set { this.name = value; } } [XmlIgnore] public string User { get { return this.user; } set { this.user = value; } } [XmlIgnore] public string Password { get { return this.password; } set { this.password = value; } } public abstract string Persist { get; } public abstract ICoreRDBHelper RDBHelper { get; } public DataSourceGeneric(string mark, string name) { NetworkChange.NetworkAddressChanged += delegate { this.ReConnect(); }; this.Mark = mark; this.Name = name; } protected virtual void ReConnect() { } public abstract void Recover(string connectionstring); public bool Save() { try { ICoreRDBHelper systemRDBHelper = RDBHelperStorage.SystemRDBHelper; bool result; if (systemRDBHelper == null) { result = false; return result; } long nextValidID = systemRDBHelper.GetNextValidID("TBSYS_DATASOURCE", "F_ID"); if (nextValidID == -1L) { result = false; return result; } StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("Insert into "); stringBuilder.Append("TBSYS_DATASOURCE"); stringBuilder.Append("( "); stringBuilder.Append("F_ID,"); stringBuilder.Append("F_NAME,"); stringBuilder.Append("F_KEY,"); stringBuilder.Append("F_ATTRIBUTE,"); stringBuilder.Append("F_DSTYPE,"); stringBuilder.Append("F_TYPE"); stringBuilder.Append(") values ("); stringBuilder.Append(nextValidID + ","); stringBuilder.Append("'" + this.Name + "',"); stringBuilder.Append("'" + this.Mark + "',"); stringBuilder.Append("'" + this.Persist + "',"); stringBuilder.Append(Convert.ToInt32(this.DSType) + ","); stringBuilder.Append(Convert.ToInt32(this.RDSType) + ")"); int num = systemRDBHelper.ExecuteSQL(stringBuilder.ToString()); if (num > 0) { result = true; return result; } result = false; return result; } catch (Exception ex) { //TraceEvent.AddException(ex); } return false; } public bool Read() { try { bool result; if (this.Mark == "") { result = false; return result; } ICoreRDBHelper systemRDBHelper = RDBHelperStorage.SystemRDBHelper; if (systemRDBHelper == null) { result = false; return result; } string commandText = "select * from TBSYS_DATASOURCE where F_KEY='" + this.Mark + "'"; DataTable dataTable = systemRDBHelper.ExecuteDatatable("TBSYS_DATASOURCE", commandText, true); if (dataTable == null) { result = false; return result; } if (dataTable.Rows.Count == 0) { result = false; return result; } DataRow dataRow = dataTable.Rows[0]; this.Name = ((dataRow["F_NAME"] == DBNull.Value) ? "" : dataRow["F_NAME"].ToString()); this.Mark = ((dataRow["F_KEY"] == DBNull.Value) ? "" : dataRow["F_KEY"].ToString()); this.RDSType = (DatabaseType)((dataRow["F_TYPE"] == DBNull.Value) ? 0 : Convert.ToInt32(dataRow["F_TYPE"])); this.Recover((dataRow["F_ATTRIBUTE"] == DBNull.Value) ? "" : dataRow["F_ATTRIBUTE"].ToString()); result = true; return result; } catch (Exception ex) { //TraceEvent.AddException(ex); } return false; } public bool Update() { try { ICoreRDBHelper systemRDBHelper = RDBHelperStorage.SystemRDBHelper; bool result; if (systemRDBHelper == null) { result = false; return result; } DataTable dataTable = systemRDBHelper.ExecuteDatatable("TBSYS_DATASOURCE", "SELECT * FROM TBSYS_DATASOURCE WHERE F_KEY='" + this.Mark + "'", false); if (dataTable == null) { result = false; return result; } dataTable.Rows[0]["F_NAME"] = this.Name; dataTable.Rows[0]["F_ATTRIBUTE"] = this.Persist; dataTable.Rows[0]["F_DSTYPE"] = Convert.ToInt32(this.DSType); dataTable.Rows[0]["F_TYPE"] = Convert.ToInt32(this.RDSType); result = systemRDBHelper.SaveTable("TBSYS_DATASOURCE", true); return result; } catch (Exception ex) { //TraceEvent.AddException(ex); } return false; } public bool UpdateEx(string newMark) { try { ICoreRDBHelper systemRDBHelper = RDBHelperStorage.SystemRDBHelper; bool result; if (systemRDBHelper == null) { result = false; return result; } DataTable dataTable = systemRDBHelper.ExecuteDatatable("TBSYS_DATASOURCE", "SELECT * FROM TBSYS_DATASOURCE WHERE F_KEY='" + this.Mark + "'", false); if (dataTable == null) { result = false; return result; } dataTable.Rows[0]["F_NAME"] = this.Name; dataTable.Rows[0]["F_ATTRIBUTE"] = this.Persist; dataTable.Rows[0]["F_DSTYPE"] = Convert.ToInt32(this.DSType); dataTable.Rows[0]["F_TYPE"] = Convert.ToInt32(this.RDSType); dataTable.Rows[0]["F_KEY"] = newMark; this.Mark = newMark; systemRDBHelper.SaveTable("TBSYS_DATASOURCE", true); result = true; return result; } catch (Exception ex) { //TraceEvent.AddException(ex); } return false; } public static bool Delete(string mark) { try { ICoreRDBHelper systemRDBHelper = RDBHelperStorage.SystemRDBHelper; bool result; if (systemRDBHelper == null) { result = false; return result; } if (systemRDBHelper.ExecuteNonQuery("DELETE FROM TBSYS_DATASOURCE WHERE F_KEY='" + mark + "'", CommandType.Text) == 0) { result = false; return result; } result = true; return result; } catch (Exception ex) { //TraceEvent.AddException(ex); } return false; } public override string ToString() { return this.Name; } } }