aboutsummaryrefslogtreecommitdiff
path: root/odb/relational/context.cxx
blob: 237ea0cf3994c466676a37252606fd0355dbf530 (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
// file      : odb/relational/context.cxx
// copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC
// license   : GNU GPL v3; see accompanying LICENSE file

#include <cassert>

#include <odb/relational/context.hxx>

using namespace std;

namespace relational
{
  context* context::current_;

  context::
  ~context ()
  {
    if (current_ == this)
      current_ = 0;
  }

  context::
  context ()
      : data_ (current ().data_),
        model (current ().model),
        generate_grow (current ().generate_grow),
        need_alias_as (current ().need_alias_as),
        insert_send_auto_id (current ().insert_send_auto_id),
        delay_freeing_statement_result (current ().delay_freeing_statement_result),
        need_image_clone (current ().need_image_clone),
        generate_bulk (current ().generate_bulk),
        global_index (current ().global_index),
        global_fkey (current ().global_fkey),
        bind_vector (data_->bind_vector_),
        truncated_vector (data_->truncated_vector_)
  {
  }

  context::
  context (data* d, sema_rel::model* m)
      : data_ (d),
        model (m),
        bind_vector (data_->bind_vector_),
        truncated_vector (data_->truncated_vector_)
  {
    assert (current_ == 0);
    current_ = this;
  }

  string context::
  index_name (qname const& table, string const& base)
  {
    string n;

    if (options.index_suffix ().count (db) != 0)
      n = base + options.index_suffix ()[db];
    else
      n = compose_name (base, "i");

    // If this database has global index names, then add the table
    // name as a prefix (the schema, if needed, will be added by
    // database-specific create_index overrides).
    //
    if (global_index)
      n = compose_name (table.uname (), n);

    return transform_name (n, sql_name_index);
  }

  string context::
  fkey_name (qname const& table, string const& base)
  {
    string n;

    if (options.fkey_suffix ().count (db) != 0)
      n = base + options.fkey_suffix ()[db];
    else
      n = compose_name (base, "fk");

    // If this database has global index names, then add the table
    // name as a prefix (the schema, if needed, will be added by
    // database-specific create_foreign_key overrides).
    //
    if (global_fkey)
      n = compose_name (table.uname (), n);

    return transform_name (n, sql_name_fkey);
  }

  string context::
  convert (string const& e, string const& c)
  {
    size_t p (c.find ("(?)"));
    string r (c, 0, p);
    r += e;
    r.append (c, p + 3, string::npos);
    return r;
  }

  string const& context::
  convert_expr (string const&, semantics::data_member&, bool)
  {
    assert (false);
  }

  bool context::
  grow_impl (semantics::class_&, user_section*)
  {
    return false;
  }

  bool context::
  grow_impl (semantics::data_member&)
  {
    return false;
  }

  bool context::
  grow_impl (semantics::data_member&,
             semantics::type&,
             const custom_cxx_type*,
             string const&)
  {
    return false;
  }

  string context::
  quote_string_impl (string const& s) const
  {
    string r;
    r.reserve (s.size ());
    r += '\'';

    for (string::size_type i (0), n (s.size ()); i < n; ++i)
    {
      if (s[i] == '\'')
        r += "''";
      else
        r += s[i];
    }

    r += '\'';
    return r;
  }

  string context::
  quote_id_impl (qname const& id) const
  {
    string r;

    bool f (true);
    for (qname::iterator i (id.begin ()); i < id.end (); ++i)
    {
      if (i->empty ())
        continue;

      if (f)
        f = false;
      else
        r += '.';

      r += '"';
      r += *i;
      r += '"';
    }

    return r;
  }
}