fastapi 0.1.18 → 0.1.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/fastapi.rb +157 -25
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 914c5c91b0fd3b9f2b81b4da4d27554b05d254a5
4
- data.tar.gz: ee22069497c8ba7c7704f60bbaf8a94af1df8ce2
3
+ metadata.gz: 91ec2057fc92dc867573e641cf9afbc4cf34908e
4
+ data.tar.gz: 3ebb509840114634d4279516fc2b939f08910f94
5
5
  SHA512:
6
- metadata.gz: 267c4b35d3474ac545f24683baa22e7e4d1012de69c4cfb7d7dad20213d59a6a0816d2c951ed9db18f80d96da1c6e192a8168dbbaf1d4870c3f82fa702040083
7
- data.tar.gz: 57fbf90fa9d58518b669aa95acec4f9b3904199e3c24609c8e0a9b0bd02e45d2fe87b10b88281302fec53e9035fc9cf9f205d009e8a9128ee81eeadc674cedb7
6
+ metadata.gz: 27e8290f2b34ac378c0b6fae534971199299c5277b49131728845ce6427e13df772ee47860515180edfc21b02eb8eefb7c2628d4e1030b4283e7ff960e93723b
7
+ data.tar.gz: 783b73a2ab2cd26af95cbededc10e887b45c0287a8992f6f65a1788f43d4286c28cdbcf6fe5a5288848be23d936799731c72c37c3cfa9c3464ffe0654e77ab71
data/lib/fastapi.rb CHANGED
@@ -37,7 +37,7 @@ class FastAPI
37
37
 
38
38
  # Create and execute an optimized SQL query based on specified filters
39
39
  #
40
- # @param fields [Array] an array containing fields to whitelist for the SQL query
40
+ # @param fields [Array] an array containing fields to whitelist for the SQL query. Can also pass in fields as arguments.
41
41
  # @return [FastAPI] the current instance
42
42
  def whitelist(fields = [])
43
43
 
@@ -312,11 +312,19 @@ class FastAPI
312
312
  currow[obj_name] = {}
313
313
  end
314
314
 
315
- currow[obj_name][field.to_sym] = api_convert_type(val, model.columns_hash[field].type)
315
+ currow[obj_name][field.to_sym] = api_convert_type(
316
+ val,
317
+ model.columns_hash[field].type,
318
+ (model.columns_hash[field].respond_to?('array') and model.columns_hash[field].array)
319
+ )
316
320
 
317
321
  elsif @model.columns_hash[field]
318
322
 
319
- currow[field.to_sym] = api_convert_type(val, @model.columns_hash[field].type)
323
+ currow[field.to_sym] = api_convert_type(
324
+ val,
325
+ @model.columns_hash[field].type,
326
+ (@model.columns_hash[field].respond_to?('array') and @model.columns_hash[field].array)
327
+ )
320
328
 
321
329
  end
322
330
 
@@ -340,6 +348,85 @@ class FastAPI
340
348
 
341
349
  end
342
350
 
351
+ # the two following methods are very similar, can reuse
352
+
353
+ def parse_postgres_array(str)
354
+
355
+ i = 0
356
+ len = str.length
357
+
358
+ values = []
359
+
360
+ i = str.index('{')
361
+
362
+ return values unless i
363
+
364
+ i = i + 1
365
+
366
+ while i < len
367
+
368
+ c = str[i]
369
+
370
+ if c == '}'
371
+
372
+ break
373
+
374
+ elsif c == '"'
375
+
376
+ i += 1
377
+ nextIndex = str.index('"', i)
378
+
379
+ while str[nextIndex - 1] == '\\'
380
+
381
+ j = 1
382
+ while str[nextIndex - j] == '\\'
383
+ j += 1
384
+ end
385
+
386
+ if j & 1 == 1
387
+ break
388
+ end
389
+
390
+ nextIndex = str.index('"', nextIndex + 1)
391
+
392
+ end
393
+
394
+ values.push str[i...nextIndex]
395
+
396
+ i = nextIndex + 1
397
+
398
+ else
399
+
400
+ if c == ','
401
+
402
+ values.push nil
403
+ i += 1
404
+ next
405
+
406
+ end
407
+
408
+ parensIndex = str.index('}', i)
409
+ nextIndex = str.index(',', i)
410
+
411
+ if nextIndex.nil? or nextIndex > parensIndex
412
+
413
+ values.push str[i...parensIndex]
414
+ break
415
+
416
+ end
417
+
418
+ values.push str[i...nexIndex]
419
+
420
+ i = nextIndex + 1
421
+
422
+ end
423
+
424
+ end
425
+
426
+ return values
427
+
428
+ end
429
+
343
430
  def parse_many(str, fields = [], types = [])
344
431
 
345
432
  rows = []
@@ -432,31 +519,37 @@ class FastAPI
432
519
  end
433
520
 
434
521
 
435
- def api_comparison(comparator, value)
522
+ def api_comparison(comparator, value, field, type, is_array)
523
+
524
+ unless is_array
525
+ field_string = field
526
+ else
527
+ field_string = 'ANY(' + field + ')'
528
+ end
436
529
 
437
530
  if comparator == 'is'
438
531
 
439
- ' = ' + ActiveRecord::Base.connection.quote(value.to_s)
532
+ ActiveRecord::Base.connection.quote(value.to_s) + ' = ' + field_string
440
533
 
441
534
  elsif comparator == 'not'
442
535
 
443
- ' <> ' + ActiveRecord::Base.connection.quote(value.to_s)
536
+ ActiveRecord::Base.connection.quote(value.to_s) + ' <> ' + field_string
444
537
 
445
538
  elsif comparator == 'gt'
446
539
 
447
- ' > ' + ActiveRecord::Base.connection.quote(value.to_s)
540
+ ActiveRecord::Base.connection.quote(value.to_s) + ' < ' + field_string
448
541
 
449
542
  elsif comparator == 'gte'
450
543
 
451
- ' >= ' + ActiveRecord::Base.connection.quote(value.to_s)
544
+ ActiveRecord::Base.connection.quote(value.to_s) + ' <= ' + field_string
452
545
 
453
546
  elsif comparator == 'lt'
454
547
 
455
- ' < ' + ActiveRecord::Base.connection.quote(value.to_s)
548
+ ActiveRecord::Base.connection.quote(value.to_s) + ' > ' + field_string
456
549
 
457
550
  elsif comparator == 'lte'
458
551
 
459
- ' <= ' + ActiveRecord::Base.connection.quote(value.to_s)
552
+ ActiveRecord::Base.connection.quote(value.to_s) + ' >= ' + field_string
460
553
 
461
554
  elsif comparator == 'in' or comparator == 'not_in'
462
555
 
@@ -470,33 +563,61 @@ class FastAPI
470
563
 
471
564
  end
472
565
 
473
- if comparator == 'in'
474
- ' IN(' + (value.map { |val| ActiveRecord::Base.connection.quote(val.to_s) }).join(',') + ')'
566
+ if is_array
567
+
568
+ type_convert = {
569
+ boolean: '::boolean',
570
+ integer: '::integer',
571
+ float: '::float'
572
+ }[type]
573
+
574
+ type_convert = '::text' if type.nil?
575
+
576
+ if comparator == 'in'
577
+ 'ARRAY[' + (value.map { |val| ActiveRecord::Base.connection.quote(val.to_s) }).join(',') + ']' + type_convert + '[] && ' + field
578
+ else
579
+ 'NOT ARRAY[' + (value.map { |val| ActiveRecord::Base.connection.quote(val.to_s) }).join(',') + ']' + type_convert + '[] && ' + field
580
+ end
581
+
475
582
  else
476
- ' NOT IN(' + (value.map { |val| ActiveRecord::Base.connection.quote(val.to_s) }).join(',') + ')'
583
+
584
+ if comparator == 'in'
585
+ field + ' IN(' + (value.map { |val| ActiveRecord::Base.connection.quote(val.to_s) }).join(',') + ')'
586
+ else
587
+ field + ' NOT IN(' + (value.map { |val| ActiveRecord::Base.connection.quote(val.to_s) }).join(',') + ')'
588
+ end
589
+
477
590
  end
478
591
 
479
592
  elsif comparator == 'contains'
480
593
 
481
- ' LIKE \'%\' || ' + ActiveRecord::Base.connection.quote(value.to_s) + ' || \'%\''
594
+ '\'%\' || ' + ActiveRecord::Base.connection.quote(value.to_s) + ' || \'%\' LIKE ' + field_string
482
595
 
483
596
  elsif comparator == 'icontains'
484
597
 
485
- ' ILIKE \'%\' || ' + ActiveRecord::Base.connection.quote(value.to_s) + ' || \'%\''
598
+ '\'%\' || ' + ActiveRecord::Base.connection.quote(value.to_s) + ' || \'%\' ILIKE ' + field_string
486
599
 
487
600
  elsif comparator == 'is_null'
488
601
 
489
- ' IS NULL'
602
+ 'NULL = ' + field_string
490
603
 
491
604
  elsif comparator == 'not_null'
492
605
 
493
- ' IS NOT NULL'
606
+ 'NOT NULL = ' + field_string
494
607
 
495
608
  end
496
609
 
497
610
  end
498
611
 
499
- def api_convert_type(val, type)
612
+ def api_convert_type(val, type, is_array = false)
613
+
614
+ return api_convert_value(val, type) unless is_array
615
+
616
+ return parse_postgres_array(val).map { |inner_value| api_convert_value(inner_value, type) }
617
+
618
+ end
619
+
620
+ def api_convert_value(val, type)
500
621
 
501
622
  if not val.nil?
502
623
  if type == :integer
@@ -691,6 +812,17 @@ class FastAPI
691
812
 
692
813
  elsif self_obj.column_names.include? field
693
814
 
815
+ base_field = self_string_table + '.' + field
816
+ field_string = base_field
817
+ is_array = false
818
+
819
+ if self_obj.columns_hash[field].respond_to?('array') and self_obj.columns_hash[field].array == true
820
+
821
+ field_string = 'ANY(' + field_string + ')'
822
+ is_array = true
823
+
824
+ end
825
+
694
826
  if self_obj.columns_hash[field].type == :boolean
695
827
 
696
828
  if !!value != value
@@ -715,9 +847,9 @@ class FastAPI
715
847
  if !!value == value
716
848
 
717
849
  if comparator == 'is'
718
- filter_array << self_string_table + '.' + field + ' IS ' + value.to_s.upcase
850
+ filter_array << value.to_s.upcase + ' = ' + field_string
719
851
  elsif comparator == 'not'
720
- filter_array << self_string_table + '.' + field + ' IS NOT ' + value.to_s.upcase
852
+ filter_array << 'NOT ' + value.to_s.upcase + ' = ' + field_string
721
853
  end
722
854
 
723
855
  end
@@ -725,19 +857,19 @@ class FastAPI
725
857
  elsif value == nil and comparator != 'is_null' and comparator != 'not_null'
726
858
 
727
859
  if comparator == 'is'
728
- filter_array << self_string_table + '.' + field + ' IS NULL'
860
+ filter_array << 'NULL = ' + field_string
729
861
  elsif comparator == 'not'
730
- filter_array << self_string_table + '.' + field + ' IS NOT NULL'
862
+ filter_array << 'NOT NULL = ' + field_string
731
863
  end
732
864
 
733
865
  elsif value.is_a? Range and comparator == 'is'
734
866
 
735
- filter_array << self_string_table + '.' + field + ' >= ' + ActiveRecord::Base.connection.quote(value.first.to_s)
736
- filter_array << self_string_table + '.' + field + ' <= ' + ActiveRecord::Base.connection.quote(value.last.to_s)
867
+ filter_array << ActiveRecord::Base.connection.quote(value.first.to_s) + ' <= ' + field_string
868
+ filter_array << ActiveRecord::Base.connection.quote(value.last.to_s) + ' >= ' + field_string
737
869
 
738
870
  else
739
871
 
740
- filter_array << self_string_table + '.' + field + api_comparison(comparator, value)
872
+ filter_array << api_comparison(comparator, value, base_field, self_obj.columns_hash[field].type, is_array)
741
873
 
742
874
  end
743
875
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fastapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.18
4
+ version: 0.1.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keith Horwood
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-16 00:00:00.000000000 Z
11
+ date: 2015-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj