aboutsummaryrefslogtreecommitdiff
path: root/mssql/stored-proc/driver.cxx
blob: 60c4d2761dae69b5c1cb08154bdefb8e1dfc00ec (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
// file      : mssql/stored-proc/driver.cxx
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

// Test SQL Server stored procedure support.
//

#include <memory>   // std::auto_ptr
#include <cassert>
#include <iostream>

#include <odb/mssql/database.hxx>
#include <odb/mssql/transaction.hxx>

#include <common/common.hxx>

#include "test.hxx"
#include "test-odb.hxx"

using namespace std;
namespace mssql = odb::mssql;
using namespace mssql;

void
create_procedure (database& db, const string& name, const string& body)
{
  transaction t (db.begin ());

  string s (db.query_value<default_schema> ().name);

  db.execute (
    "IF EXISTS ("
    "  SELECT * FROM sysobjects"
    "    WHERE name = '" + name + "' AND user_name(uid) = '" + s +"')"
    "  DROP PROCEDURE [" + s + "].[" + name + "]");

  db.execute ("CREATE PROCEDURE [" + s + "].[" + name + "] " + body);

  t.commit ();
}

int
main (int argc, char* argv[])
{
  try
  {
    auto_ptr<database> db (create_specific_database<database> (argc, argv));

    object o1 (1, "a");
    object o2 (2, "b");
    object o3 (3, "c");

    {
      transaction t (db->begin ());
      db->persist (o1);
      db->persist (o2);
      db->persist (o3);
      t.commit ();
    }

    {
      create_procedure (
        *db, "select_all_objects",
        "AS"
        "  SELECT num, str FROM mssql_stored_proc_object ORDER BY id;");

      typedef odb::result<select_all_objects> result;

      transaction t (db->begin ());

      result r (db->query<select_all_objects> ());

      for (result::iterator i (r.begin ()); i != r.end (); ++i)
        cout << i->num << " " << i->str << endl;
      cout << endl;

      t.commit ();
    }

    {
      create_procedure (
        *db, "select_objects",
        "(@id INT, @n VARCHAR(512))"
        "AS"
        "  SELECT str FROM mssql_stored_proc_object "
        "    WHERE [id] = @id OR [num] = @n ORDER BY id;");

      typedef mssql::query<select_objects> query;
      typedef odb::result<select_objects> result;

      transaction t (db->begin ());

      result r (db->query<select_objects> (
                  query::_val (o1.id) + "," + query::_val (o2.num)));

      for (result::iterator i (r.begin ()); i != r.end (); ++i)
        cout << i->str << endl;
      cout << endl;

      t.commit ();
    }

    {
      create_procedure (
        *db, "objects_min_max",
        "(@min INT = NULL OUTPUT, @max INT = NULL OUTPUT)"
        "AS"
        "  SELECT @min = MIN(num), @max = MAX(num)"
        "    FROM mssql_stored_proc_object;");

      create_procedure (
        *db, "objects_min_max_odb",
        "AS"
        "  DECLARE @min INT, @max INT;"
        "  EXEC objects_min_max @min OUTPUT, @max OUTPUT;"
        "  SELECT @min, @max;");

      transaction t (db->begin ());

      objects_min_max omm (db->query_value<objects_min_max> ());
      cout << omm.num_min << " " << omm.num_max << endl
           << endl;

      t.commit ();
    }

    {
      create_procedure (
        *db, "insert_object",
        "(@n INT, @s VARCHAR(512), @id INT = NULL OUTPUT)"
        "AS"
        "  INSERT INTO mssql_stored_proc_object([num], [str])"
        "    VALUES(@n, @s);"
        "  SET @id = SCOPE_IDENTITY();"
        "  RETURN 123;");

      create_procedure (
        *db, "insert_object_odb",
        "(@n INT, @s VARCHAR(512))"
        "AS"
        "  DECLARE @id INT;"
        "  DECLARE @ret INT;"
        "  DECLARE @tbl TABLE(dummy INT);"
        "  EXEC @ret = insert_object @n, @s, @id OUTPUT;"
        "  SELECT @ret, @id;");

      // An alternative implementation that produces a different
      // result set configuration at the ODBC level.
      //
      /*
      create_procedure (
        *db, "insert_object_odb",
        "(@n INT, @s VARCHAR(512))"
        "AS"
        "  DECLARE @id INT;"
        "  DECLARE @ret INT;"
        "  DECLARE @tbl TABLE(dummy INT);"
        "  INSERT INTO @tbl EXEC @ret = insert_object @n, @s, @id OUTPUT;"
        "  SELECT @ret, @id;");
      */

      typedef mssql::query<select_objects> query;

      transaction t (db->begin ());

      insert_object io (
        db->query_value<insert_object> (
          query::_val (4) + "," + query::_val ("d")));

      cout << io.ret << " " << io.id << endl
           << endl;

      t.commit ();
    }
  }
  catch (const odb::exception& e)
  {
    cerr << e.what () << endl;
    return 1;
  }
}