/********************************************************************************************* * Copyright 2004 - Volian Enterprises, Inc. All rights reserved. * Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE * ------------------------------------------------------------------------------ * $Workfile: vdb_ROUsage.cs $ $Revision: 6 $ * $Author: Kathy $ $Date: 5/01/06 11:17a $ * * $History: vdb_ROUsage.cs $ * * ***************** Version 6 ***************** * User: Kathy Date: 5/01/06 Time: 11:17a * Updated in $/LibSource/VDB * Fix B2006-018: single quote in proc number causes problem on data * request * * ***************** Version 5 ***************** * User: Kathy Date: 8/16/05 Time: 2:54p * Updated in $/LibSource/VDB * B2005-030: error if missing ndx * * ***************** Version 4 ***************** * User: Kathy Date: 4/21/05 Time: 10:19a * Updated in $/LibSource/VDB * always write inf file & remove upgrade2005 define * * ***************** Version 3 ***************** * User: Kathy Date: 3/08/05 Time: 1:48p * Updated in $/LibSource/VDB * Approval * * ***************** Version 2 ***************** * User: Jsj Date: 8/23/04 Time: 10:12a * Updated in $/LibSource/VDB * Fixed replace of single quote * * ***************** Version 1 ***************** * User: Kathy Date: 7/27/04 Time: 8:40a * Created in $/LibSource/VDB *********************************************************************************************/ using System; using System.Data; using System.Text; using System.Collections.Specialized; using System.IO; using System.Windows.Forms; using VDB; using VDB_ConnType; namespace VDB_ROUsage { /// /// Open, Create, Delete, Update functions for RO Usage files (tables) /// public class vdb_ROUsage:VDB_Base { public enum SortTypes {NotSorted,ProcSort,ROIDsort}; // list of sorting types public string UsageROndx = ""; public string UsagROIDndx = ""; public vdb_ROUsage(String strPath):base(strPath) { // if no table (file name) specified, then default to USAGERO (USAGERO.DBF) if (DBTable.Equals("")) DBTable = "USAGERO"; } // Build a SQL statement. Optionally sort and filter the select statement. private void BuildSelectCommand(int SortingType,string WhereStr) { StringBuilder tmp = new StringBuilder(); tmp.Append("SELECT [NUMBER], [SEQUENCE], [INSTANCE], [ROID] FROM ["); tmp.Append(DBTable); tmp.Append("]"); if (WhereStr.Length > 0) // Was a WHERE clause passed in? { // Add " WHERE " to the beginning if it is not already there string tmpstr = WhereStr; //.ToUpper(); if (tmpstr[0] != ' ') { tmp.Append(" "); if (!tmpstr.StartsWith("WHERE ")) tmp.Append("WHERE "); } else if (!tmpstr.StartsWith(" WHERE ")) tmp.Append(" WHERE"); // add the passed in WHERE clause tmp.Append(tmpstr); } switch (SortingType) { case 0: //NotSorted break; case 1: // ProcSort - sorted by procedure number tmp.Append(" GROUP BY [NUMBER],[SEQUENCE],[INSTANCE],[ROID]"); break; case 2: // ROIDsort - sorted by ROID tmp.Append(" GROUP BY [ROID],[NUMBER],[SEQUENCE],[INSTANCE]"); break; } SelectCmd = tmp.ToString(); } private void BuildNDXFileNames() { UsageROndx = DBTable; UsagROIDndx = DBTable.Substring(0,4)+"ROID"; } // Build the SQL command needed to update a row in the USAGERO file public override string GetRecUpdateStr(DataTable tbl,DataRow row) { int j = 0; bool FirstOne = true; StringBuilder updateStr = new StringBuilder(); string strNumber = ""; string strSequence = ""; string strInstance = ""; updateStr.Append("UPDATE ["); updateStr.Append(DBTable); updateStr.Append("] SET"); foreach (DataColumn col in tbl.Columns) { bool isString = (col.DataType == Type.GetType("System.String")); if (col.ColumnName.Equals("NUMBER")) { strNumber = row.ItemArray[j].ToString(); strNumber = strNumber.Replace("'","''"); } else if (col.ColumnName.Equals("SEQUENCE")) { strSequence = row.ItemArray[j].ToString(); strSequence = strSequence.Replace("'","''"); } else if (col.ColumnName.Equals("INSTANCE")) { strInstance = row.ItemArray[j].ToString(); strInstance = strInstance.Replace("'","''"); } if (FirstOne) { updateStr.Append(" ["); FirstOne = false; } else updateStr.Append(", ["); updateStr.Append(col.ColumnName); updateStr.Append("]="); if (row.ItemArray[j].ToString().Equals("")) { updateStr.Append("NULL"); } else if (isString) { String tmpstr = row.ItemArray[j].ToString(); updateStr.Append("'"); tmpstr = tmpstr.Replace("'","''"); updateStr.Append(tmpstr); updateStr.Append("'"); } j++; } // Add the WHERE clause updateStr.Append(" WHERE [NUMBER] = '"); updateStr.Append(strNumber.ToString()); updateStr.Append("' AND [SEQUENCE] = '"); updateStr.Append(strSequence.ToString()); updateStr.Append("' AND [INSTANCE] = '"); updateStr.Append(strInstance.ToString()); updateStr.Append("'"); return updateStr.ToString(); } // Build the SQL command needed to insert a row in the USAGERO file public override string GetRecInsertStr(DataTable tbl,DataRow row) { int j = 0; StringBuilder insrtStr = new StringBuilder(); StringBuilder valueStr = new StringBuilder(); insrtStr.Append("INSERT INTO ["); insrtStr.Append(DBTable); insrtStr.Append("] ("); valueStr.Append(" VALUES ("); foreach (DataColumn col in tbl.Columns) { bool isString = (col.DataType == Type.GetType("System.String")); insrtStr.Append("["); insrtStr.Append(col.ColumnName); insrtStr.Append("],"); if (row.ItemArray[j].ToString().Equals("")) { valueStr.Append("NULL"); } else if (isString) { String tmpstr = row.ItemArray[j].ToString(); tmpstr = tmpstr.Replace("'","''"); valueStr.Append("'"); valueStr.Append(tmpstr); valueStr.Append("'"); } else { valueStr.Append(row.ItemArray[j].ToString()); } valueStr.Append(","); j++; } insrtStr = insrtStr.Replace(',',')',insrtStr.Length-1,1); valueStr = valueStr.Replace(',',')',valueStr.Length-1,1); insrtStr.Append(valueStr.ToString()); return insrtStr.ToString(); } // Build the SQL command needed to delete a row in the USAGERO file public override string RecDeleteStr(DataTable tbl,DataRow row) { int j = 0; string strNumber = ""; string strSequence = ""; string strInstance = ""; StringBuilder deleteStr = new StringBuilder(); deleteStr.Append("DELETE FROM ["); deleteStr.Append(DBTable); deleteStr.Append("]"); row.RejectChanges(); foreach (DataColumn col in tbl.Columns) { if (col.ColumnName.Equals("NUMBER")) { strNumber = row.ItemArray[j].ToString(); strNumber = strNumber.Replace("'","''"); } else if (col.ColumnName.Equals("SEQUENCE")) { strSequence = row.ItemArray[j].ToString(); strSequence = strSequence.Replace("'","''"); } else if (col.ColumnName.Equals("INSTANCE")) { strInstance = row.ItemArray[j].ToString(); strInstance = strInstance.Replace("'","''"); } j++; } // Add the WHERE clause deleteStr.Append(" WHERE [NUMBER] = '"); deleteStr.Append(strNumber.ToString()); deleteStr.Append("' AND [SEQUENCE] = '"); deleteStr.Append(strSequence.ToString()); deleteStr.Append("' AND [INSTANCE] = '"); deleteStr.Append(strInstance.ToString()); deleteStr.Append("'"); return deleteStr.ToString(); } public override string GetCreateIndx1String() { StringBuilder index1Str = new StringBuilder(); index1Str.Append("CREATE INDEX ["); index1Str.Append(UsageROndx); index1Str.Append("] ON "); index1Str.Append(DBTable); index1Str.Append(".DBF ([NUMBER],[SEQUENCE],[INSTANCE])"); return (index1Str.ToString()); } public override string GetCreateIndx2String() { StringBuilder index2Str = new StringBuilder(); index2Str.Append("CREATE INDEX ["); index2Str.Append(UsagROIDndx); index2Str.Append("] ON "); index2Str.Append(DBTable); index2Str.Append(".DBF ([ROID])"); return (index2Str.ToString()); } public override bool IndexFilesExist() { if (UsageROndx==""||UsagROIDndx=="") BuildNDXFileNames(); if(!File.Exists(DBPath+"\\"+UsageROndx+".NDX") || !File.Exists(DBPath+"\\"+UsagROIDndx+".NDX")) return false; return true; } public override void DeleteIndexFiles() { if(File.Exists(DBPath+"\\"+UsageROndx+".NDX")) File.Delete(DBPath+"\\"+UsageROndx+".NDX"); if(File.Exists(DBPath+"\\"+UsagROIDndx+".NDX")) File.Delete(DBPath+"\\"+UsagROIDndx+".NDX"); if(File.Exists(DBPath+"\\"+DBTable+".INF")) File.Delete(DBPath+"\\"+DBTable+".INF"); } public override StringCollection CreateIndexFilesStatements() { if (isdBaseFile()) { StringCollection rtnStrColl = new StringCollection(); string indx1 = GetCreateIndx1String(); rtnStrColl.Add(indx1); string indx2 = GetCreateIndx2String(); rtnStrColl.Add(indx2); return rtnStrColl; } return null; } // Build a list of SQL commands needed to create a new USAGERO table (file) // and add the first row public override StringCollection CreateTableStatements(string strTblName) { StringCollection rtnStrColl = new StringCollection(); StringBuilder createStr = new StringBuilder(); //StringBuilder index1Str = new StringBuilder(); //StringBuilder index2Str = new StringBuilder(); if (strTblName.Length > 0) // was a table name passd in? { DBTable = strTblName; } if (DBTable.Equals("")) { MessageBox.Show("Trying to Create a new Table without a Name","Create Table Error"); return rtnStrColl; } // Build the command that creates a new SET table (file) createStr.Append("CREATE TABLE ["); createStr.Append(DBTable); createStr.Append("] "); createStr.Append("([NUMBER] Char(20), [SEQUENCE] Char(12), "); createStr.Append("[INSTANCE] Char(1), [ROID] Char(16))"); rtnStrColl.Add(createStr.ToString()); // add create table if (isdBaseFile()) { // build the ndx file names (w/o extensions) // BuildNDXFileNames(); // Build the command that creates the first index file rtnStrColl.Add(GetCreateIndx1String()); // add create 1st index // Build the commmand that creates the second index file string tmp = DBTable; rtnStrColl.Add(GetCreateIndx2String()); // add create 2nd index } return rtnStrColl; } // After the table is created, create the associated INF file public override int PostCreateTableFunction() { CreateINF(); // should already be created return 0; } // This is called in the class constructor and // if the table name is assigned to the DBTable property public override int CreateINF() { // if we are using dBase files, create an INF file containing // a list of the associated NDX files. if (isdBaseFile()) { StreamWriter infFile; string infFileStr; StringBuilder tmp = new StringBuilder(); // build the ndx file names (w/o extensions) BuildNDXFileNames(); //build INF file name tmp.Append(DBPath); tmp.Append("\\"); tmp.Append(DBTable); tmp.Append(".INF"); infFileStr = tmp.ToString(); // if the INF file does not already exist create it //if (!File.Exists(infFileStr)) //{ // always recreate it. Some plants' data had invalid files // and it was felt that it would be quicker to recreate always // rather than infFile = new StreamWriter(infFileStr,false); infFile.Write("NDX1="); infFile.Write(UsageROndx); // USAGERO.NDX infFile.Write(".NDX\r\n"); infFile.Write("NDX2="); infFile.Write(UsagROIDndx); //USAGROID.NDX infFile.Write(".NDX\r\n"); infFile.Close(); //} } return 0; } // return a list of SQL commands that will drop (delete) a database table (file) public override StringCollection DeleteTableStatements(string strTblName) { StringCollection rtnStrColl = new StringCollection(); StringBuilder DropTableStr = new StringBuilder(); DropTableStr.Append("Drop Table ["); DropTableStr.Append(DBTable); DropTableStr.Append("]"); rtnStrColl.Add(DropTableStr.ToString()); return rtnStrColl; } // return a dataset of RO Usage records, not sorted public System.Data.DataSet GetNotSorted(string WhereStr) { BuildSelectCommand((int)SortTypes.NotSorted,WhereStr); return DB_Data; } // return a dataset of RO Usage records, sorted by the [NUMBER] field public System.Data.DataSet GetSortedByProc(string WhereStr) { BuildSelectCommand((int)SortTypes.ProcSort,WhereStr); return DB_Data; } // return a dataset of RO Usage records, sorted by the [ROID] field public System.Data.DataSet GetSortedByROID(string WhereStr) { BuildSelectCommand((int)SortTypes.ROIDsort,WhereStr); return DB_Data; } public void UpdateProcNumber(string oldnum, string newnum) { // Change fromnumber first, then tonumber StringBuilder updateStr = new StringBuilder(); updateStr.Append("UPDATE ["); updateStr.Append(DBTable); updateStr.Append("] SET [NUMBER] = '"); updateStr.Append(newnum.Replace("'","''")); updateStr.Append("' WHERE [NUMBER] = '"); updateStr.Append(oldnum.Replace("'","''")); updateStr.Append("'"); ProcessACommand(updateStr.ToString()); } } }