Class: Rooq::QueryValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/rooq/query_validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(tables) ⇒ QueryValidator

Returns a new instance of QueryValidator.



5
6
7
8
9
# File 'lib/rooq/query_validator.rb', line 5

def initialize(tables)
  @tables = tables.each_with_object({}) do |table, hash|
    hash[table.name] = table
  end
end

Instance Method Details

#validate_delete(query) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rooq/query_validator.rb', line 95

def validate_delete(query)
  errors = []

  table_error = validate_table(query.table)
  errors << table_error if table_error

  if query.conditions
    condition_errors = validate_condition(query.conditions)
    errors.concat(condition_errors)
  end

  raise QueryValidationError.new(errors) unless errors.empty?

  true
end

#validate_insert(query) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rooq/query_validator.rb', line 54

def validate_insert(query)
  errors = []

  table_error = validate_table(query.table)
  errors << table_error if table_error

  query.column_list.each do |field|
    next unless field.is_a?(Field)

    field_error = validate_field(field)
    errors << field_error if field_error
  end

  raise QueryValidationError.new(errors) unless errors.empty?

  true
end

#validate_select(query) ⇒ Object



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
# File 'lib/rooq/query_validator.rb', line 11

def validate_select(query)
  errors = []

  # Validate FROM table
  if query.from_table
    table_error = validate_table(query.from_table)
    errors << table_error if table_error
  end

  # Validate selected fields
  query.selected_fields.each do |field|
    next unless field.is_a?(Field)

    field_error = validate_field(field)
    errors << field_error if field_error
  end

  # Validate conditions
  if query.conditions
    condition_errors = validate_condition(query.conditions)
    errors.concat(condition_errors)
  end

  # Validate order specs
  query.order_specs.each do |spec|
    field_error = validate_field(spec.field)
    errors << field_error if field_error
  end

  # Validate joins
  query.joins.each do |join|
    table_error = validate_table(join.table)
    errors << table_error if table_error

    condition_errors = validate_condition(join.condition)
    errors.concat(condition_errors)
  end

  raise QueryValidationError.new(errors) unless errors.empty?

  true
end

#validate_update(query) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rooq/query_validator.rb', line 72

def validate_update(query)
  errors = []

  table_error = validate_table(query.table)
  errors << table_error if table_error

  query.set_values.each_key do |field|
    next unless field.is_a?(Field)

    field_error = validate_field(field)
    errors << field_error if field_error
  end

  if query.conditions
    condition_errors = validate_condition(query.conditions)
    errors.concat(condition_errors)
  end

  raise QueryValidationError.new(errors) unless errors.empty?

  true
end