summaryrefslogtreecommitdiff
path: root/src/utils/xb_import.cpp
blob: 272a0d0d1e4bc29644ffc7bdf935394e09c3326a (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*  xb_import.cpp

XBase64 Software Library

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

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

Email Contact:

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

*/

#include <xbase.h>
using namespace xb;


struct sFldMap{
  xbUInt32 iRecPos;
  xbInt16  iFldNo;
  char     cFldType;
  sFldMap  *next;
};

void PrintHelp();
void PrintHelp(){
  std::cout << "Usage: xb_execsql [-h] [-?] [--help] [-v] [--version] -i infilename.txt  -d delimeter -t table.DBF -q --quiet"  << std::endl << std::endl;
  std::cout << "This program imports data from a text file into a specified DBF file/table." << std::endl << std::endl;
}
void PrintVersion();
void PrintVersion(){
  std::cout << "Xbase64 Version: " << xbase_VERSION_MAJOR << "." << xbase_VERSION_MINOR << "." << xbase_VERSION_PATCH << std::endl;
}



int main(int argc, char* argv[])
{
  xbXBase  x;
  xbSql    sql( &x );
  xbFile   f( sql.GetXbasePtr() );
  xbInt16  iRc        = XB_NO_ERROR;
  xbString sFileName  = "";
  xbString sTableName = "";
  xbString sSqlLine   = "";
  xbString sParm      = "";
  xbString sMsg;
  xbString sLine;
  xbString sFld;
  xbBool   bQuiet     = xbFalse;
  char     cDelimiter = ',';
  char     cType      = ' ';
  sFldMap  *fmFldList = 0;
  sFldMap  *fmTemp    = 0;
  xbInt16  iFldNo     = 0;
  xbUInt32 ulRecCtr   = 0;
  xbBool   bRecUpdated;
  x.EnableMsgLogging();


  if (argc < 2 || x.GetCmdLineOpt( argc, argv, "-h", sParm ) ||
                  x.GetCmdLineOpt( argc, argv, "-?", sParm ) ||
                  x.GetCmdLineOpt( argc, argv, "--help", sParm )){
    PrintHelp();
    return 1;
  }

  if ( x.GetCmdLineOpt( argc, argv, "-v", sParm ) ||
       x.GetCmdLineOpt( argc, argv, "--version", sParm )){
    PrintVersion();
    return 1;
  }

  if ( x.GetCmdLineOpt( argc, argv, "-q", sParm ) ||
       x.GetCmdLineOpt( argc, argv, "--quiet", sParm )){
    bQuiet = xbTrue;
  }

  if ( x.GetCmdLineOpt( argc, argv, "-d", sParm )){
    if( sParm.Len() > 0 ){
      cDelimiter = sParm[1];
    }
  }

  if( !x.GetCmdLineOpt( argc, argv, "-i", sFileName ) || sFileName == "" ){
    PrintHelp();
    return 1;
  }

  if( !x.GetCmdLineOpt( argc, argv, "-t", sTableName ) || sTableName == "" ){
    PrintHelp();
    return 1;
  }

  xbDbf *MyFile = NULL;
  if(( iRc = x.OpenHighestVersion( sTableName.Str(), "", &MyFile )) != XB_NO_ERROR ){
    std::cout << "Could not open table/file RC = " << iRc  << " file = "  << sTableName.Str() << std::endl;
    x.DisplayError( iRc );
    return 1;
  }

  if(( iRc = f.xbFopen( "r", sFileName, XB_SINGLE_USER )) != XB_NO_ERROR ){
    sMsg.Sprintf( "Error opening [%s]\n", sFileName.Str() );
    std::cout << sMsg.Str();
    x.DisplayError( iRc );
    return 1;
  }


  if(( iRc = f.xbFgets( 1024, sLine )) != XB_NO_ERROR ){
    sMsg.Sprintf( "Error reading [%s]\n", sFileName.Str() );
    std::cout << sMsg.Str();
    x.DisplayError( iRc );
    return 1;
  }


  // determine how many fields in a record
  xbUInt32 lFldCnt = sLine.CountChar( cDelimiter, 1 );
  //  std::cout << "in rec = [" << sLine.Str() << "]\n";
  //  std::cout << "fld cnt = [" << lFldCnt << "]\n";

  // do the mapping between field names in source data and field numbers in target table
  for( xbUInt32 l = 0; l < (lFldCnt + 1); l++ ){

    // get the field
    sFld.ExtractElement( sLine.Str(), cDelimiter, l+1, 1 );
    sFld.ZapTrailingChar( 0x0a );   // eliminate CRLF
    sFld.ZapTrailingChar( 0x0d );   // eliminate CRLF


    // do the lookup
    // std::cout << "processing field [" << l << "] [" << sFld.Str() << "]\n";
    // if found, create an entry in the field list structure
    // else if not quiet, display a message

    //iRc = MyFile->GetFieldNo( sFld, &iFldNo );

    if(( iRc = MyFile->GetFieldNo( sFld, iFldNo )) == XB_NO_ERROR ){
      MyFile->GetFieldType( iFldNo, cType );
      fmTemp = (sFldMap *) calloc( 1, sizeof( sFldMap ));
      if( !fmTemp ){
        std::cout << "Memory allocation error\n";
        exit(1);
      } else {
        fmTemp->iRecPos = l;
        fmTemp->iFldNo = iFldNo;
        fmTemp->cFldType = cType;
        fmTemp->next = fmFldList;
        fmFldList = fmTemp;

      }
    } else {
      if( !bQuiet ){
        std::cout << "Field [" << sFld.Str() << "] not found in target table" << std::endl;
      }
    }
  }


  while( f.xbFgets( 1024, sLine ) == XB_NO_ERROR ){
    bRecUpdated = xbFalse;
    // std::cout << sLine.Str() << "\n";

    if(( iRc = MyFile->BlankRecord()) != XB_NO_ERROR ){
      sMsg.Sprintf( "MyFile->BlankRecord() error [%d]\n", iRc );
      std::cout << sMsg.Str();
      x.DisplayError( iRc );
    } else {

      fmTemp = fmFldList;
      while( fmTemp ){ 

        // std::cout << "*** RecPos = " << fmTemp->iRecPos  << "   FldNo  = " << fmTemp->iFldNo  << "  Type = " << fmTemp->cFldType << "\n";

        sFld.ExtractElement( sLine.Str(), cDelimiter, fmTemp->iRecPos+1, 1 );
        sFld.ZapTrailingChar( 0x0a );   // eliminate CRLF
        sFld.ZapTrailingChar( 0x0d );   // eliminate CRLF

        // remove any matching leading and trailing quotes
        if( sFld[1] == '\'' && sFld[sFld.Len()] == '\'' ){
          sFld.ZapTrailingChar( '\'' );
          sFld.ZapLeadingChar ( '\'' );
          //std::cout << "DataNq   = " << sFld.Str()       << "\n";
        } else if( sFld[1] == '"' && sFld[sFld.Len()] == '"' ){
          sFld.ZapTrailingChar( '"' );
          sFld.ZapLeadingChar ( '"' );
        }

        // std::cout << "Data   = " << sFld.Str()       << "\n";
        if( sFld.Len() > 0 ){
          bRecUpdated = xbTrue;
          if( fmTemp->cFldType == 'C' || fmTemp->cFldType == 'L' || fmTemp->cFldType == 'D' || fmTemp->cFldType == 'N' || fmTemp->cFldType == 'F' ){
            iRc = MyFile->PutField( fmTemp->iFldNo, sFld );
          } else if( fmTemp->cFldType == 'M' ){
            iRc = MyFile->UpdateMemoField( fmTemp->iFldNo, sFld );
          } else {
            std::cout << "Field type [" << fmTemp->cFldType << "] not built yet" << std::endl;
          }

          if( iRc != XB_NO_ERROR  && !bQuiet ){
            std::cout << "Error [" << iRc << "] on field [" << fmTemp->iFldNo << "] on record [" << ulRecCtr << "]" << std::endl;
          }
        }
        fmTemp = fmTemp->next;
      }

      if( bRecUpdated ){
        iRc = MyFile->AppendRecord();
        if( iRc != XB_NO_ERROR ){
          if( !bQuiet ){
            std::cout << "Error [" << iRc << "] on appending record [" << ulRecCtr << "]" << std::endl;
          }
          MyFile->Abort();
        } else {
          iRc = MyFile->Commit();
          if( iRc != XB_NO_ERROR ){
            if( !bQuiet ){
              std::cout << "Error [" << iRc << "] on appending record [" << ulRecCtr << "]" << std::endl;
            }
            MyFile->Abort();
          }
        }
      }

    }

    ulRecCtr++;

  }





  f.xbFclose();
  return 0;
}