summaryrefslogtreecommitdiff
path: root/src/sql/xbinsert.cpp
blob: 835f9e6ed4eaa83de60f6d60a1fdcd30c3ba43e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* xbinsert.cpp

XBase64 Software Library

Copyright (c) 1997,2003,2014,2022 Gary A Kunkel

The xb64 software library is covered under the terms of the GPL Version 3, 2007 license.

Email Contact:

    XDB-devel@lists.sourceforge.net
    XDB-users@lists.sourceforge.net

*/

#include "xbase.h"

#ifdef XB_SQL_SUPPORT

namespace xb{


/***********************************************************************/
xbInt16 xbSql::SqlInsert( const xbString &sCmdLine ){

  // expected format:
  //  INSERT INTO tablename (field1, field2, field3,...) VALUES ( 'charval', numval, 'what is the correct odbc date format to use? CCYYMMDD');

  xbInt16 iRc = 0;
  xbInt16 iErrorStop = 0;
  xbString sTableName;
  xbDbf * dbf = NULL;

  xbString sWork1;
  xbString sFieldList;
  xbString sDataList;
  xbString sFieldName;
  xbString sFieldData;

  // queue the memo data to post after the append occurs
  // dbase does not support usage of memo fields with insert commands
  xbLinkList <xbInt16>  llMemoFieldNos;
  xbLinkList <xbString> llMemoFieldData;

  try{
    //  retrieve table name
    sTableName.ExtractElement( sCmdLine, ' ', 3, 0 );
    sTableName.Trim();

    //  if not open, attempt to open it
    dbf = xbase->GetDbfPtr( sTableName );

    if( !dbf ){
      if(( iRc = xbase->OpenHighestVersion( sTableName, "", &dbf )) != XB_NO_ERROR ){
        iErrorStop = 100;
        throw iRc;
      }
    }

    if( !dbf ){
      iErrorStop = 110;
      iRc = XB_FILE_NOT_FOUND;
      throw iRc;
    }

    //  blank the record buffer
    dbf->BlankRecord();

    sWork1.ExtractElement( sCmdLine, ')', 1, 0 );
    sFieldList.ExtractElement( sWork1, '(', 2, 0 );

    sDataList.ExtractElement( sCmdLine, '(', 3, 0 );
    sDataList.Trim();
    sDataList.ZapTrailingChar( ')' );

    xbUInt32 iFldCnt = sFieldList.CountChar( ',' );
    xbUInt32 iDataCnt = sDataList.CountChar( ',', 1 );

    // verify there are the same count in the field list and values list
    if( iFldCnt != iDataCnt ){
      iErrorStop = 120;
      iRc = XB_SYNTAX_ERROR;
      throw iRc;
    }

    iFldCnt++;
    xbInt16 iFldNo = -1;
    char cFldType = 0x00;
    for( xbUInt32 i = 1; i <= iFldCnt; i++ ){
      sFieldName.ExtractElement( sFieldList, ',', i, 0 );
      sFieldName.Trim();
      if(( iRc = dbf->GetFieldNo( sFieldName, iFldNo )) != XB_NO_ERROR ){
        iErrorStop = 130;
        throw iRc;
      }
      if(( iRc = dbf->GetFieldType( iFldNo, cFldType )) != XB_NO_ERROR ){
        iErrorStop = 140;
        throw iRc;
      }

      // get the field data here
      sFieldData.ExtractElement( sDataList, ',', i, 1 );
      sFieldData.Trim();

      // remove beginning and ending quotes
      if(( sFieldData[1] == '\'' && sFieldData[sFieldData.Len()] == '\'') || (sFieldData[1] == '"' && sFieldData[sFieldData.Len()] == '"' )){
        sFieldData.Remove( sFieldData.Len(), 1 );
        sFieldData.Remove( 1, 1 );
      }

      switch( cFldType ){
        case 'C':
        case 'N':
        case 'L':
          if(( iRc = dbf->PutField( iFldNo, sFieldData )) != XB_NO_ERROR ){
            iErrorStop = 150;
            throw iRc;
          }
          break;

        case 'D':
          // assumes input date format of yyyy-mm-dd
          if( sFieldData.Len() != 10 || sFieldData[5] != '-' || sFieldData[8] != '-' ){
            iErrorStop = 160;
            iRc = XB_INVALID_DATA;
            throw iRc;
          }
          sWork1 = sFieldData;
          sWork1.Remove( 8, 1 );
          sWork1.Remove( 5, 1 );
          if(( iRc = dbf->PutField( iFldNo, sWork1 )) != XB_NO_ERROR ){
            iErrorStop = 170;
            throw iRc;
          }
          break;

        case 'M':
          llMemoFieldNos.InsertAtFront( iFldNo );
          llMemoFieldData.InsertAtFront( sFieldData );
          break;

        default:
          iErrorStop= 180;
          iRc = XB_INVALID_FIELD_TYPE;
          throw iRc;
      }
    }

    if(( iRc = dbf->AppendRecord()) != XB_NO_ERROR ){
      iErrorStop = 190;
      throw iRc;
    }

    // Add any memo fields
    xbLinkListNode<xbInt16>  * llN = llMemoFieldNos.GetHeadNode();
    xbLinkListNode<xbString> * llD = llMemoFieldData.GetHeadNode();
    xbUInt32 ulCnt = llMemoFieldNos.GetNodeCnt();
    for( xbUInt32 i = 0; i < ulCnt; i++ ){
      iFldNo = llN->GetKey();
      sFieldData = llD->GetKey();
      if(( iRc = dbf->UpdateMemoField( iFldNo, sFieldData )) != XB_NO_ERROR ){
        iErrorStop = 200;
        throw iRc;
       }
      llN = llN->GetNextNode();
      llD = llD->GetNextNode();
    }

    if(( iRc = dbf->Commit()) != XB_NO_ERROR ){
      iErrorStop = 210;
      throw iRc;
    }
  }
  catch (xbInt16 iRc ){
    xbString sMsg;
    xbase->WriteLogMessage( sCmdLine );
    sMsg.Sprintf( "xbSql::SqlInsert() Exception Caught. Error Stop = [%d] rc = [%d] table = [%s] field = [%s] data = [%s]", iErrorStop, iRc, sTableName.Str(), sFieldName.Str(), sFieldData.Str() );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
    if( dbf )
      dbf->Abort();
  }
  return iRc;

}

/***********************************************************************/
}              /* namespace       */
#endif         /*  XB_SQL_SUPPORT */