⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqlbasewhere.cpp

📁 应用VC++编的简单的ODBC访问接口。(我已经是会员了! 挖哈哈!!!)
💻 CPP
字号:

#include "SQLBaseWhere.h"

#include <sstream>
#include <iomanip>
using namespace std;

#include "Dt.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSQLBaseWhere::CSQLBaseWhere() : CSQLBase()
{
}

CSQLBaseWhere::~CSQLBaseWhere()
{
}

const char* CSQLBaseWhere::GetStatement()
{
   _temp = _stmt;
   if ( _where.length() > 0 )
   {
      _temp += " where ";
      _temp += _where;
   }
   return _temp.c_str();
}

void CSQLBaseWhere::ResetContent()
{
   _where = "";
   _temp  = "";
   CSQLBase::ResetContent();
}

void CSQLBaseWhere::SetWhere(const char * a_szWhere)
{
   if ( _where != "" )
      _where += " and ";
   _where += a_szWhere;
}

void CSQLBaseWhere::SetWhereOr(const char * a_szWhere)
{
   if ( _where != "" )
      _where += " or ";
   _where += a_szWhere;
}

void CSQLBaseWhere::SetWhere( const char* columnName, int value )
{
   if ( _where != "" )
      _where += " and ";
   _where += columnName;
   _where += "=";

   /* char ach[32]; wsprintf( ach, "%d", value ); _where += ach; */
   ostringstream ss;
   ss << fixed << setprecision(2) << value;
   _where += ss.str(); // unfinished: test this
}

void CSQLBaseWhere::SetWhere( const char* columnName, long value, char a_DataType )
{
   //assert( a_DataType == CSQLBaseWhere::LONG || a_DataType == CSQLBaseWhere::ODBC_DATE || a_DataType == CSQLBaseWhere::JULIAN_DATE);
   if ( _where != "" )
      _where += " and ";
   _where += columnName;
   _where += "=";

   if ( a_DataType == CSQLBaseWhere::LONG )
   {
      /* char ach[32]; wsprintf( ach, "%ld", value ); _where += ach; */
      ostringstream ss;
      ss << dec << value;
      _where += ss.str(); // unfinished: test this
   }
   else if ( a_DataType == CSQLBaseWhere::ODBC_DATE 
      || a_DataType == CSQLBaseWhere::JULIAN_DATE )
   {
      TDt dt( value );
      string cs = dt.Format( TDt::ODBC );
      _where += cs;
   }
}

void CSQLBaseWhere::SetWhere( const char* columnName, double value )
{
   if ( _where != "" )
      _where += " and ";
   _where += columnName;
   _where += "=";

   /* char ach[32]; wsprintf( ach, "%.2f", value ); _where += ach; */
   ostringstream ss;
   ss << fixed << setprecision(2) << value;
   _where += ss.str(); // unfinished: test this
}

void CSQLBaseWhere::SetWhere( const char* columnName, bool isValidTime, int month, int day, int year )
{
   if ( _where != "" )
      _where += " and ";

   string cs;
   if ( ! isValidTime )
   {
      cs = "isnull(";
      cs += columnName;
      cs += ")";
   }
   else
   {
      TDt dt( month, day, year );
      cs = columnName;
      cs += "=";
      cs += dt.Format( TDt::ODBC );
   }

   _where += cs;
}


void CSQLBaseWhere::SetWhere( const char* columnName, const char* a_value, 
   char a_DataType, bool a_bUseLike )
{
   if ( _where != "" )
      _where += " and ";

   // Test for NULL immediately because it has to have 
   // a different syntax
   string value = a_value;
   if ( value == "" || value == "NULL" || value == "null" )
   {
      _where += "isnull(";
      _where += columnName;
      _where += ")";
      return;
   }

   // add the column name to the conditional
   _where += columnName;

   // handle a wildcard match
   if ( a_bUseLike && a_DataType == CSQLBaseWhere::STRING )
      _where += " like ";
   else
      _where += "=";

   //
   // Add the remaining data to the right side of the 
   // comparison.  Check if it's an ODBC date string 
   // and process it as is.
   if ( value[0] == '{' && value[1] == 'd' && value[2] == '\'' ) 
   {  
      _where += value; // {d'1972-01-15'} etc.
      return;
   }

   if ( a_DataType == CSQLBaseWhere::STRING )
   {
      ConvertQuotes( value );
      _where += "'";
      _where += value;
      _where += "'";
      return;
   }

   if ( a_DataType == CSQLBaseWhere::ORACLE_DATE )
   {
      TDt dt( value.c_str() );
      string cs = dt.Format( TDt::ORACLE );
      _where += "'";
      _where += cs;
      _where += "'";
      return;
   }
   
   if ( a_DataType == CSQLBaseWhere::ODBC_DATE )
   {
      TDt dt( value.c_str() );
      string cs = dt.Format( TDt::ODBC );
      _where += cs;
      return;
   }

   _where += value;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -