summaryrefslogtreecommitdiffstats
path: root/rasodmg/oqlquery.cc
blob: 4b1a3f13606c03def4cfcecc57b72a2a29f06879 (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
/
/**
 * SOURCE:   oqlquery.cc
 *
 * MODULE:   rasodmg
 * CLASS:    r_OQL_Query
 * FUNCTION: r_oql_execute()
 *
 * COMMENTS:
 *		None
*/

static const char rcsid[] = "@(#)rasodmg, r_OQL_Query and r_oql_execute(): $Id: oqlquery.cc,v 1.25 2002/08/28 12:21:57 coman Exp $";

#include "rasodmg/oqlquery.hh"

#include <string.h>
#include <ctype.h>     // isdigit()
#ifdef __VISUALC__
#include <strstrea.h>
#else
#include <strstream>
#endif

#ifdef __VISUALC__
#ifndef __EXECUTABLE__
#define __EXECUTABLE__
#define OQLQUERY_NOT_SET
#endif
#endif

#include "rasodmg/database.hh"
#include "rasodmg/transaction.hh"
#include "rasodmg/set.hh"
#include "rasodmg/gmarray.hh"
  
#include "raslib/rmdebug.hh"
#include "clientcomm/clientcomm.hh"

#ifdef OQLQUERY_NOT_SET
#undef __EXECUTABLE__
#endif
 
r_OQL_Query::r_OQL_Query()
  : queryString(0),
    parameterizedQueryString(0),
    mddConstants(0)
{
}


r_OQL_Query::r_OQL_Query( const char* s )
  : queryString(0),
    mddConstants(0)
{
  parameterizedQueryString = new char[strlen(s)+1];
  strcpy( parameterizedQueryString, s );
  
  reset_query();
}


r_OQL_Query::r_OQL_Query( const r_OQL_Query& q )
  : queryString(0),
    parameterizedQueryString(0),
    mddConstants(0)
{
  // copy the query string
  if(q.queryString) {
	queryString = new char[strlen(q.queryString)+1];
	strcpy( queryString, q.queryString); 
  }

  // copy the parameterized query string
  if(q.parameterizedQueryString) {
	parameterizedQueryString = new char[strlen(q.parameterizedQueryString)+1];
	strcpy( parameterizedQueryString, q.parameterizedQueryString );
  }

  if( q.mddConstants ) {
    mddConstants = new r_Set< r_GMarray* >( *(q.mddConstants) );
  }
}


r_OQL_Query::~r_OQL_Query()
{
  if( queryString )
    delete[] queryString;
  queryString = 0;
  
  if( parameterizedQueryString )
    delete[] parameterizedQueryString;
  parameterizedQueryString = 0;

  if( mddConstants )
    delete mddConstants;
  mddConstants = 0;
}


const r_OQL_Query& 
r_OQL_Query::operator=( const r_OQL_Query& q )
{
  if( this != &q )
  {
    // clean up and copy the query string

    if( queryString )
    {
      delete[] queryString;
      queryString = 0;
    }

    if( q.queryString )
    {
      queryString = new char[strlen(q.queryString)+1];
      strcpy( queryString, q.queryString ); 
    }

    if( mddConstants )
    {
      delete mddConstants;
      mddConstants = 0;
    }

    if( q.mddConstants )
    {
      mddConstants = new r_Set< r_GMarray* >( *(q.mddConstants) );
    }

    // clean up and copy the parameterized query string  
    if( parameterizedQueryString )
    {
      delete[] parameterizedQueryString;
      parameterizedQueryString = 0;
    }

    if( q.parameterizedQueryString )
    {
      parameterizedQueryString = new char[strlen(q.parameterizedQueryString)+1];
      strcpy( parameterizedQueryString, q.parameterizedQueryString );
    }
  }
  
  return *this;
}
   

r_OQL_Query&
r_OQL_Query::operator<<( const char* s ) throw( r_Error )
{
  try
  {
    replaceNextArgument( s );
  }
  catch( ... )
  {
    throw;
  }

  return *this;
}
   
   
r_OQL_Query&
r_OQL_Query::operator<<( r_Char c ) throw( r_Error )
{
  char valueString[2];
  
  valueString[0] = c;
  valueString[1] = '\0';
   
  try
  {
    replaceNextArgument( valueString );
  }
  catch( ... )
  {
    throw;
  }

  return *this;
}
   
   
r_OQL_Query&
r_OQL_Query::operator<<( r_Short s ) throw( r_Error )
{
  char valueString[256];
  
  std::ostrstream valueStream( valueString, 256 ); 
   
  valueStream << s << std::ends;

  try
  {
    replaceNextArgument( valueString );
  }
  catch( ... )
  {
    throw;
  }

  return *this;
}
   
   
r_OQL_Query&
r_OQL_Query::operator<<( r_UShort us ) throw( r_Error )
{
  char valueString[256];
  
  std::ostrstream valueStream( valueString, 256 ); 
   
  valueStream << us << std::ends;

  try
  {
    replaceNextArgument( valueString );
  }
  catch( ... )
  {
    throw;
  }

  return *this;
}
   
   
r_OQL_Query&
r_OQL_Query::operator<<( r_Long l ) throw( r_Error )
{
  char valueString[256];
  
  std::ostrstream valueStream( valueString, 256 ); 
   
  valueStream << l << std::ends;

  try
  {
    replaceNextArgument( valueString );
  }
  catch( ... )
  {
    throw;
  }

  return *this;
}
   
   
r_OQL_Query&
r_OQL_Query::operator<<( r_ULong ul ) throw( r_Error )
{
  char valueString[256];
  
  std::ostrstream valueStream( valueString, 256 ); 
   
  valueStream << ul << std::ends;

  try
  {
    replaceNextArgument( valueString );
  }
  catch( ... )
  {
    throw;
  }

  return *this;
}
   
   
r_OQL_Query&
r_OQL_Query::operator<<( r_Point pt ) throw( r_Error )
{
  char valueString[256];
  
  std::ostrstream valueStream( valueString, 256 ); 
   
  valueStream << pt << std::ends;

  try
  {
    replaceNextArgument( valueString );
  }
  catch( ... )
  {
    throw;
  }

  return *this;
}
   
   
r_OQL_Query&
r_OQL_Query::operator<<( r_Sinterval in ) throw( r_Error )
{
  char valueString[256];
  
  std::ostrstream valueStream( valueString, 256 ); 
   
  valueStream << in << std::ends;

  try
  {
    replaceNextArgument( valueString );
  }
  catch( ... )
  {
    throw;
  }

  return *this;
}
   
   
r_OQL_Query&
r_OQL_Query::operator<<( r_Minterval in ) throw( r_Error )
{
  char valueString[256];
  
  std::ostrstream valueStream( valueString, 256 ); 
   
  valueStream << in << std::ends;

  try
  {
    replaceNextArgument( valueString );
  }
  catch( ... )
  {
    throw;
  }

  return *this;
}
   
   
r_OQL_Query&
r_OQL_Query::operator<<( r_GMarray& in ) throw( r_Error )
{
  // determine number of next mdd (starting with 0)
  unsigned long mddNo = 0;
  if( mddConstants )
    mddNo = mddConstants->cardinality();

  char valueString[256];  
  std::ostrstream valueStream( valueString, 256 ); 
  valueStream << "#MDD" << mddNo << "#" << std::ends;

  try
  {
    replaceNextArgument( valueString );
  }
  catch( ... )
  {
    throw;
  }

  // save reference to in
  if( !mddConstants )
    mddConstants = new r_Set< r_GMarray* >();

  mddConstants->insert_element( &in ); 

  return *this;
}
   
   


int
r_OQL_Query::is_update_query() const
{
  return !is_retrieval_query();
}



int
r_OQL_Query::is_retrieval_query() const
{
  int returnValue = 0;

  if( parameterizedQueryString )
    returnValue = strstr( parameterizedQueryString, "select" ) ||
                  strstr( parameterizedQueryString, "SELECT" );

  return returnValue;
}

    
 
void
r_OQL_Query::reset_query()
{
  if( queryString )
    delete[] queryString;
  
  queryString = new char[strlen(parameterizedQueryString)+1];
  strcpy( queryString, parameterizedQueryString );

  if( mddConstants )
  {
    delete mddConstants;
    mddConstants = 0;
  }
}


void
r_OQL_Query::replaceNextArgument( const char* valueString )
  throw( r_Error )
{
  char* argumentBegin=NULL;
  char* argumentEnd=NULL;
  char* argumentVal=NULL;
  int   argumentLength=0;
  char* tmpString=NULL;
  int   length=0;
          
  // locate the next argument in the query string

  argumentBegin = argumentEnd = strchr( queryString, '$' );
  
  if( !argumentBegin )
  {
    r_Error err = r_Error( r_Error::r_Error_QueryParameterCountInvalid );
    throw err;
  }

  //  while( *argumentEnd != ' ' && *argumentEnd != '\0' )
  argumentEnd++;
  
  //is digit or invalid argument format
  if(!isdigit(*argumentEnd))
     throw  r_Error( QUERYPARAMETERINVALID );
  
  while( isdigit(*argumentEnd) && *argumentEnd != ' ' && *argumentEnd != '\0' )
    argumentEnd++;

  argumentLength = argumentEnd - argumentBegin;
  argumentVal	 = new char[ argumentLength + 1];
  strncpy(argumentVal, argumentBegin, argumentLength);
  argumentVal[argumentLength] = '\0';

  while(true)
  {
   // allocate a new query string and fill it
   tmpString   = queryString;
   length      = strlen(queryString) - argumentLength + strlen(valueString);
   queryString = new char[ length + 1 ];
  
   *argumentBegin = '\0';
   std::ostrstream queryStream( queryString, length + 1 ); 
   
   queryStream << tmpString << valueString << argumentEnd << std::ends;
  
   //update the reference
   argumentEnd=queryString+strlen(tmpString)+strlen(valueString);
   
   //remove buffer
   delete[] tmpString;

   //search again for this parameter   
   argumentEnd = argumentBegin = strstr( argumentEnd, argumentVal );

   //end string?
   if(argumentBegin == NULL)
     break;

   //skip $
   argumentEnd++;
  
   //is digit or invalid argument format
   if(!isdigit(*argumentEnd))
   {
     delete [] argumentVal;
     throw  r_Error( QUERYPARAMETERINVALID );
   }
   
   //skip digits
   while( isdigit(*argumentEnd) && *argumentEnd != ' ' && *argumentEnd != '\0' )
     argumentEnd++;     
  }
  
  delete[] argumentVal;
}



// HP COMPILER BUG
// The iterator type needs a typedef because otherwise the compiler creates a duplicate
// definition for each instance of the following template function.
typedef r_Iterator<r_GMarray*> r_Iterator_r_GMarray; 

void r_oql_execute( r_OQL_Query& query, r_Set< r_Ref_Any > &result )
  throw( r_Error )
{
  if( r_Database::actual_database == 0 || r_Database::actual_database->get_status() == r_Database::not_open )
  {
    r_Error err = r_Error( r_Error::r_Error_DatabaseClosed );
    throw err;
  }
     
  if( r_Transaction::actual_transaction == 0 || r_Transaction::actual_transaction->get_status() != r_Transaction::active )
  {
    r_Error err = r_Error( r_Error::r_Error_TransactionNotOpen );
    throw err;
  }

  try
  {
    r_Database::actual_database->getComm()->executeQuery( query, result );
  }   
  catch( ... )
  {
    throw;  // re-throw the exception
  }
  
  // reset the arguments of the query object
  query.reset_query();
}



void r_oql_execute( r_OQL_Query& query, r_Set< r_Ref< r_GMarray > > &result )
  throw( r_Error )
{
  if( r_Database::actual_database == 0 || r_Database::actual_database->get_status() == r_Database::not_open )
  {
    r_Error err = r_Error( r_Error::r_Error_DatabaseClosed );
    throw err;
  }
     
  if( r_Transaction::actual_transaction == 0 || r_Transaction::actual_transaction->get_status() != r_Transaction::active )
  {
    r_Error err = r_Error( r_Error::r_Error_TransactionNotOpen );
    throw err;
  }

  try
  {
    r_Set< r_Ref_Any > genericSet;
    

    r_Database::actual_database->getComm()->executeQuery( query, genericSet );

    const r_Type* typeSchema = genericSet.get_element_type_schema();

    if( !typeSchema || typeSchema->type_id() != r_Type::MARRAYTYPE )
    {
	  r_Error err( r_Error::r_Error_TypeInvalid );
	  throw err;
	}
    
    //
    // iterate through the generic set and build a specific one
    //
    result.set_type_by_name( genericSet.get_type_name() );
    result.set_type_structure( genericSet.get_type_structure() );

    r_Iterator< r_Ref_Any > iter;
    for( iter=genericSet.create_iterator(); iter.not_done(); iter++ )
      result.insert_element( r_Ref<r_GMarray>(*iter) );      
  }   
  catch( ... )
  {
    throw;  // re-throw the exception
  }
  
  // reset the arguments of the query object
  query.reset_query();
}



void r_oql_execute( r_OQL_Query& query )
  throw( r_Error )
{
  if( r_Database::actual_database == 0 || r_Database::actual_database->get_status() == r_Database::not_open )
  {
    r_Error err = r_Error( r_Error::r_Error_DatabaseClosed );
    throw err;
  }
     
  if( r_Transaction::actual_transaction == 0 || r_Transaction::actual_transaction->get_status() != r_Transaction::active )
  {
    r_Error err = r_Error( r_Error::r_Error_TransactionNotOpen );
    throw err;
  }

  try
  {
    r_Database::actual_database->getComm()->executeQuery( query );
  }   
  catch( ... )
  {
    throw;  // re-throw the exception
  }
  
  // reset the arguments of the query object
  query.reset_query();
}