Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cSQLCmd.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cSQLCmd_H
7 #define _INC_cSQLCmd_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cSQLConfig.h"
13 #include "../Variant/cVariant.h"
14 #include "../Variant/cVarMap.h"
16 
17 namespace GrayLib
18 {
19  class GRAYLIB_LINK cVarMap;
20 
22  {
25 
33  };
34 
36  {
40 
41  public:
42  static const char* k_pszNULL; // = "NULL";
43 
44  protected:
47 
48  public:
49  cSQLCmd(cStringA sTableName = "", cStringA sCommand = "");
50  virtual ~cSQLCmd();
51 
52  virtual SQLCMD_TYPE get_CmdType() const = 0;
53  virtual bool isResultsExpected() const
54  {
56  return false;
57  }
58 
60  {
62  return m_sTable;
63  }
64  void put_Table(cStringA sTable)
65  {
67  ResetCommand0(); // m_sCommand must be rebuilt after this.
68  m_sTable = sTable;
69  }
70 
71  bool isCommandMade() const
72  {
73  return ! m_sCommand.IsEmpty();
74  }
76  {
78  return m_sCommand;
79  }
80  void put_Command(cStringA sCommand)
81  {
83  m_sCommand = sCommand;
84  }
86  {
87  m_sCommand = ""; // just this must be rebuilt.
88  }
89  virtual void ResetCommand();
90  virtual void MakeCommand(const cSQLConfigCmd& cfg) = 0;
91 
92  static bool GRAYCALL IsValueTime(const char* pszVal);
93  static cStringA GRAYCALL MakeValueTime(const cSQLConfigCmd& cfg, const cTimeDouble& date);
94  static cStringA GRAYCALL MakeValueStr(const cSQLConfigCmd& cfg, const cVariant& vVal);
95  };
96 
98  {
102  SQL_COMPARE_GreaterThan, // > -- greater than
103  SQL_COMPARE_LessThan, // < -- less than
104  SQL_COMPARE_GreaterThanE, // >= -- greater than or equal to
105  SQL_COMPARE_LessThanE, // <= -- less than or equal to
106  SQL_COMPARE_NotEqual, // <> -- not equal to
107  SQL_COMPARE_Like, // wildcard string matching.
111  };
112 
114  {
118 
119  public:
120  static const char* const k_SQLCompares[SQL_COMPARE_QTY];
121  static const char* const k_Param; // = "?"; // Place holder for param. For late rendering based on driver used.
122 
123  private:
124  cStringA m_sWhere;
125  cArrayStruct<cVariant> m_aParams;
126 
127  public:
128  cSQLCmdWhere();
129 
130  void SetWhereParenthesis();
131 
132  void AddWhereCmd(const char* pszWhereCmd, bool bUseOr = false);
133  void AddWhereV(const char* pszColumnName, const char* pszValue = nullptr, SQL_COMPARE_TYPE eCmp = SQL_COMPARE_IS_NULL, bool bUseOr = false);
134 
135  void AddWhereInt(const char* pszColumnName, int value, SQL_COMPARE_TYPE eCmp = SQL_COMPARE_Equals, bool bUseOr = false);
136  void AddWhereUInt(const char* pszColumnName, UINT value, SQL_COMPARE_TYPE eCmp = SQL_COMPARE_Equals, bool bUseOr = false);
137  void AddWhereDouble(const char* pszColumnName, double value, SQL_COMPARE_TYPE eCmp = SQL_COMPARE_Equals, bool bUseOr = false);
138  void AddWhereTime(const char* pszColumnName, const cTimeDouble& date, SQL_COMPARE_TYPE eCmp = SQL_COMPARE_Equals, bool bUseOr = false);
139  void AddWhereStr(const char* pszColumnName, const char* pszValue, SQL_COMPARE_TYPE eCmp = SQL_COMPARE_Equals, bool bUseOr = false);
140 
141  cStringA MakeCommandWhere(const cSQLConfigCmd& cmd, bool bIncludeWhereLiteral = true);
142  void ResetCommandWhere();
143  };
144 
146  {
150 
151  typedef cSQLCmd SUPER_t;
152 
153  private:
154  cArrayStruct<cSQLColumnInfo> m_aColumnInfo;
155 
156  public:
157  cSQLCmdCreate(cStringA sTableName);
158  virtual ~cSQLCmdCreate();
159 
160  virtual SQLCMD_TYPE get_CmdType() const override { return SQLCMD_Create; }
161 
162  void AddColumnInfo(cStringA sName, VARTYPE_TYPE eVarType, INT16 iScale, bool bNullable);
163 
164  virtual void ResetCommand() override;
165  virtual void MakeCommand(const cSQLConfigCmd& cfg) override;
166  };
167 
169  {
172  typedef cSQLCmd SUPER_t;
173  public:
174  cSQLCmdDrop(cStringA sTableName);
175  virtual SQLCMD_TYPE get_CmdType() const override { return SQLCMD_Drop; }
176  virtual void MakeCommand(const cSQLConfigCmd& cfg) override;
177  };
178 
180  {
185 
186  typedef cSQLCmd SUPER_t;
187 
188  public:
190 
191  protected:
196 
197  public:
198  cSQLCmdSelect(cStringA sFromTable);
199  virtual ~cSQLCmdSelect();
200 
201  virtual SQLCMD_TYPE get_CmdType() const override { return SQLCMD_Select; }
202  virtual bool isResultsExpected() const
203  {
204  return true;
205  }
206 
207  void SetCount(const char* pszColumnName = nullptr);
208  void SetCountWhereInt(const char* pszColumnName, int iValue);
209 
210  void AddSelectCol(const char* pszColumnName);
211  void _cdecl SetSelectColsF(const char* pszColumnName, ...);
212  void put_Select(cStringA sSelect);
213 
214  void put_GroupBy(cStringA sColumnName);
215  void put_OrderBy(cStringA sColumnName);
216  void put_Limit(int iLimit=1);
217 
218  virtual void ResetCommand() override;
219  virtual void MakeCommand(const cSQLConfigCmd& cfg) override;
220  };
221 
223  {
227 
228  typedef cSQLCmd SUPER_t;
229  public:
231  public:
232  cSQLCmdDelete(cStringA sTable);
233 
234  virtual SQLCMD_TYPE get_CmdType() const override { return SQLCMD_Delete; }
235 
236  virtual void ResetCommand() override
237  {
238  m_Where.ResetCommandWhere();
239  SUPER_t::ResetCommand();
240  }
241 
242  virtual void MakeCommand(const cSQLConfigCmd& cfg) override;
243  };
244 
246  {
249  typedef cSQLCmd SUPER_t;
250 
251  public:
253 
254  public:
255  cSQLCmdInsertBase(cStringA sTableName);
256  virtual void ResetCommand() override;
257  void MakeCommandInsert(const cSQLConfigCmd& cfg, bool bFieldNames = true);
258 
259  bool HasColumns() const
260  {
261  return(m_Values.m_aTuples.GetSize() > 0);
262  }
263  void AddColumn(const char* pszColumnName, const cVariant& vVal)
264  {
265  ResetCommand0(); // m_sCommand must be rebuilt after this.
266  m_Values.SetValVar(pszColumnName, vVal);
267  }
268  };
269 
271  : public cSQLCmdInsertBase
272  {
278 
279  typedef cSQLCmdInsertBase SUPER_t;
280  public:
281  cSQLCmdInsert(cStringA sTable = "");
282  virtual SQLCMD_TYPE get_CmdType() const override { return SQLCMD_Insert; }
283  virtual void MakeCommand(const cSQLConfigCmd& cfg) override;
284  };
285 
287  : public cSQLCmdInsertBase
288  {
292 
293  typedef cSQLCmdInsertBase SUPER_t;
294  public:
296 
297  public:
298  explicit cSQLCmdUpdate(cStringA sTableName = "");
299 
300  virtual SQLCMD_TYPE get_CmdType() const override { return SQLCMD_Update; }
301 
302  virtual void ResetCommand() override;
303  virtual void MakeCommand(const cSQLConfigCmd& cfg) override;
304  };
305 };
306 
307 #endif // _INC_cSQLCmd_H
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
Definition: cSQLCmd.h:146
virtual SQLCMD_TYPE get_CmdType() const override
Definition: cSQLCmd.h:160
Definition: cSQLCmd.h:223
virtual SQLCMD_TYPE get_CmdType() const override
Definition: cSQLCmd.h:234
cSQLCmdWhere m_Where
Definition: cSQLCmd.h:230
virtual void ResetCommand() override
Definition: cSQLCmd.h:236
Definition: cSQLCmd.h:169
virtual SQLCMD_TYPE get_CmdType() const override
Definition: cSQLCmd.h:175
Definition: cSQLCmd.h:246
cVarMap m_Values
Named Fields and Values to set. allow me to poke stuff into this list directly. Similar to m_aParams ...
Definition: cSQLCmd.h:252
void AddColumn(const char *pszColumnName, const cVariant &vVal)
Definition: cSQLCmd.h:263
bool HasColumns() const
Definition: cSQLCmd.h:259
Definition: cSQLCmd.h:272
virtual SQLCMD_TYPE get_CmdType() const override
Definition: cSQLCmd.h:282
Definition: cSQLCmd.h:180
cStringA m_sGroupBy
Definition: cSQLCmd.h:193
cSQLCmdWhere m_Where
Definition: cSQLCmd.h:189
virtual bool isResultsExpected() const
Definition: cSQLCmd.h:202
cStringA m_sOrderBy
Definition: cSQLCmd.h:194
cStringA m_sSelect
What fields should be returned from Table and joins?
Definition: cSQLCmd.h:192
virtual SQLCMD_TYPE get_CmdType() const override
Definition: cSQLCmd.h:201
ITERATE_t m_iLimit
'TOP' if MSSQL or 'LIMIT' if MySQL. Should always be used with 'order by'. m_sOrderBy
Definition: cSQLCmd.h:195
Definition: cSQLCmd.h:288
virtual SQLCMD_TYPE get_CmdType() const override
Definition: cSQLCmd.h:300
cSQLCmdWhere m_Where
What records am i updating?
Definition: cSQLCmd.h:295
Definition: cSQLCmd.h:114
void ResetCommandWhere()
Definition: cSQLCmdWhere.cpp:192
static const char *const k_Param
Definition: cSQLCmd.h:121
Definition: cSQLCmd.h:36
cStringA get_Command() const
Definition: cSQLCmd.h:75
virtual void MakeCommand(const cSQLConfigCmd &cfg)=0
build the string from our args.
void put_Table(cStringA sTable)
Definition: cSQLCmd.h:64
void put_Command(cStringA sCommand)
Definition: cSQLCmd.h:80
virtual bool isResultsExpected() const
Definition: cSQLCmd.h:53
static const char * k_pszNULL
Definition: cSQLCmd.h:42
virtual SQLCMD_TYPE get_CmdType() const =0
cStringA m_sTable
The table ( 'From' if select ), delete, create.
Definition: cSQLCmd.h:45
cStringA m_sCommand
The resultant full SQL command. use MakeCommand() to build this up.
Definition: cSQLCmd.h:46
bool isCommandMade() const
Definition: cSQLCmd.h:71
cStringA get_Table() const
Definition: cSQLCmd.h:59
void ResetCommand0()
Definition: cSQLCmd.h:85
Definition: cSQLConfig.h:55
Definition: cVarMap.h:109
cArraySortHash< cVarTuple, ATOMCODE_t > m_aTuples
Hash sorted by ATOMCODE_t. NOT alphabetic sort.
Definition: cVarMap.h:115
HRESULT SetValVar(const ATOMCHAR_t *pszKey, const cVariant &vVal)
Definition: cVarMap.cpp:303
Definition: cVariant.h:26
bool IsEmpty() const noexcept
Definition: cString.h:176
Definition: cTimeDouble.h:21
Definition: cMesh.h:22
VARTYPE_TYPE
< define types of structure/record elements. (COM uses VARTYPE=VARENUM for this) stored as BYTE
Definition: cVariantType.h:19
class __DECL_IMPORT cSQLCmdWhere
Definition: cSQLDatabase.h:22
SQLCMD_TYPE
Definition: cSQLCmd.h:22
@ SQLCMD_Delete
Delete selected rows.
Definition: cSQLCmd.h:30
@ SQLCMD_Create
Create a table.
Definition: cSQLCmd.h:27
@ SQLCMD_Update
Update existing row in the db. (similar to select + insert)
Definition: cSQLCmd.h:32
@ SQLCMD_Select
Return a selected set based on where criteria.
Definition: cSQLCmd.h:29
@ SQLCMD_Drop
Drop a table.
Definition: cSQLCmd.h:28
@ SQLCMD_Insert
Write a row into the db.
Definition: cSQLCmd.h:31
@ SQLCMD_SProc
Exec a stored procedure. May (or may not) return stuff or do anything.
Definition: cSQLCmd.h:26
class __DECL_IMPORT cSQLCmd
Definition: cSQLDatabase.h:20
SQL_COMPARE_TYPE
Definition: cSQLCmd.h:98
@ SQL_COMPARE_LessThan
Definition: cSQLCmd.h:103
@ SQL_COMPARE_Equals
Definition: cSQLCmd.h:101
@ SQL_COMPARE_Like
Definition: cSQLCmd.h:107
@ SQL_COMPARE_LessThanE
Definition: cSQLCmd.h:105
@ SQL_COMPARE_GreaterThanE
Definition: cSQLCmd.h:104
@ SQL_COMPARE_IS_NULL
Definition: cSQLCmd.h:108
@ SQL_COMPARE_IS_NOT_NULL
Definition: cSQLCmd.h:109
@ SQL_COMPARE_QTY
Definition: cSQLCmd.h:110
@ SQL_COMPARE_NotEqual
Definition: cSQLCmd.h:106
@ SQL_COMPARE_GreaterThan
Definition: cSQLCmd.h:102
class __DECL_IMPORT cSQLCmdUpdate
Definition: cSQLDatabase.h:21
int ITERATE_t
like size_t but signed
Definition: Index.h:28