256 lines
7.8 KiB
C#

/*********************************************************************************************
* Copyright 2004 - Volian Enterprises, Inc. All rights reserved.
* Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
* ------------------------------------------------------------------------------
* $Workfile: vdb_SetExt.cs $ $Revision: 2 $
* $Author: Jsj $ $Date: 8/23/04 10:12a $
*
* $History: vdb_SetExt.cs $
*
* ***************** 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.Windows.Forms;
using VDB;
namespace VDB_SetExt
{
/// <summary>
/// Open, Create, Delete, Update functions for SETEXT files (tables)
/// (set extension)
/// </summary>
public class vdb_SetExt:VDB_Base
{
public vdb_SetExt(String strPath):base(strPath)
{
// if no table (file name) specified, then default to SETEXT (SET.DBF)
if (DBTable.Equals(""))
DBTable = "SETEXT";
}
// Build a SQL statement for selecting the data. The select for the set extension
// datta is much more involved because the date field for the approved date needs
// calculated/formatted.
public override string DefaultSelectCommand()
{
StringBuilder tmp = new StringBuilder();
tmp.Append("select t3.COMMENT, t3.REV, t3.PC, t3.RECID,format(ApprovedDate,'m/d/yyyy hh:nn:ss') ");
tmp.Append(" as ApprovedDate from(select COMMENT, REV, PC, RECID,iif(MyDate=DateValue('1/1/1970'),null,MyDate) ");
tmp.Append(" as ApprovedDate from (select COMMENT, REV, PC, RECID, dateadd(\"S\",(((asc(mid(tdstamp,4,1))*256)+asc(mid(tdstamp,3,1)) )");
tmp.Append("*256+asc(mid(tdstamp,2,1)) )*256+asc(mid(tdstamp,1,1)),datevalue('1/1/1970')) as MyDate ");
tmp.Append(" from (select COMMENT, REV, PC, RECID,iif(isnull(tdstamp),chr(0)+chr(0)+chr(0)+chr(0),left(tdstamp+chr(0)+chr(0)+chr(0)+chr(0),4)) as tdstamp from setext)t1)t2)t3 ");
SelectCmd = tmp.ToString();
return SelectCmd;
}
// Build the SQL command needed to update a row in the SETEXT file
public override string GetRecUpdateStr(DataTable tbl,DataRow row)
{
int j = 0;
StringBuilder updateStr = new StringBuilder();
StringBuilder LikeRecID = new StringBuilder();
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.ToUpper().Equals("RECID"))
{
LikeRecID.Append("___"); // ignore the first 3 positions
LikeRecID.Append(row.ItemArray[j].ToString(),3,5);
}
//only comment is updated (at least for initial 32bit veproms
// browser. This may need modified if it is used later
if (col.ColumnName.ToUpper().Equals("COMMENT"))
{
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("'");
}
else
{
updateStr.Append(row.ItemArray[j].ToString());
}
}
j++;
}
updateStr.Append(" WHERE [RECID] LIKE '");
updateStr.Append(LikeRecID.ToString());
updateStr.Append("'");
return updateStr.ToString();
}
// Build the SQL command needed to insert a row in the SET 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)
{
// the select statement was modified to handle the tdstamp field (which
// is a calculated date). This needs to be handled on insert, i.e. the
// fieldname.
bool isString = (col.DataType == Type.GetType("System.String"));
bool isApprovedDate = col.ColumnName.ToUpper().Equals("APPROVEDDATE");
insrtStr.Append("[");
if(!isApprovedDate)
insrtStr.Append(col.ColumnName);
else
insrtStr.Append("TDSTAMP");
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 SET file
public override string RecDeleteStr(DataTable tbl,DataRow row)
{
int j = 0;
String RecIDStr = "";
StringBuilder deleteStr = new StringBuilder();
deleteStr.Append("DELETE FROM [");
deleteStr.Append(DBTable);
deleteStr.Append("] WHERE ");
row.RejectChanges();
foreach (DataColumn col in tbl.Columns)
{
if (col.ColumnName.Equals("RECID"))
{
RecIDStr = row.ItemArray[j].ToString();
}
j++;
}
deleteStr.Append("[RECID] = '");
deleteStr.Append(RecIDStr);
deleteStr.Append("'");
return deleteStr.ToString();
}
// Build a list of SQL commands needed to create a new SET table (file)
// and add the first row
public override StringCollection CreateTableStatements(string strTblName)
{
StringCollection rtnStrColl = new StringCollection();
StringBuilder createStr = 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("([COMMENT] Char(250), [REV] Char(30), [PC] Char(10), ");
createStr.Append("[RECID] Char(8), [TDSTAMP] Int)");
rtnStrColl.Add(createStr.ToString()); // add create table
return rtnStrColl;
}
// Build a list of SQL commands needed to create the first data row
public override StringCollection CreateFirstRecordStatement(string strTblName)
{
StringCollection rtnStrColl = new StringCollection();
StringBuilder firstRecStr = new StringBuilder();
// Build the command that adds the first (blank) record
// with the RECID initialize to 00000001
firstRecStr.Append("INSERT INTO [");
firstRecStr.Append(DBTable);
firstRecStr.Append("] ([RECID]) VALUES ('00000001')");
rtnStrColl.Add(firstRecStr.ToString()); // add inserst first record
return rtnStrColl;
}
// 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;
}
}
}