aboutsummaryrefslogtreecommitdiff
path: root/odb/sqlite/statement.cxx
blob: e7d197bc99024b22cd6b1c0aa434729b6f1a156c (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
// file      : odb/sqlite/statement.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#include <odb/sqlite/statement.hxx>
#include <odb/sqlite/connection.hxx>
#include <odb/sqlite/error.hxx>

using namespace std;

namespace odb
{
  namespace sqlite
  {
    // statement
    //

    statement::
    statement (connection& conn, const string& s)
        : conn_ (conn)
    {
      if (int e = sqlite3_prepare_v2 (
            conn_.handle (),
            s.c_str (),
            static_cast<int> (s.size () + 1),
            &stmt_,
            0))
      {
        translate_error (e, conn_);
      }
    }

    statement::
    statement (connection& conn, const char* s, std::size_t n)
        : conn_ (conn)
    {
      if (int e = sqlite3_prepare_v2 (
            conn_.handle (),
            s,
            static_cast<int> (n),
            &stmt_,
            0))
      {
        translate_error (e, conn_);
      }
    }



    statement::
    ~statement ()
    {
      sqlite3_finalize (stmt_);
    }

    // simple_statement
    //

    simple_statement::
    simple_statement (connection& conn, const string& s)
        : statement (conn, s),
          result_set_ (stmt_ ? sqlite3_column_count (stmt_) != 0: false)
    {
    }

    simple_statement::
    simple_statement (connection& conn, const char* s, std::size_t n)
        : statement (conn, s, n),
          result_set_ (stmt_ ? sqlite3_column_count (stmt_) != 0: false)
    {
    }

    unsigned long long simple_statement::
    execute ()
    {
      if (stmt_ == 0) // Empty statement or comment.
        return 0;

      if (int e = sqlite3_reset (stmt_))
        translate_error (e, conn_);

      unsigned long long r (0);

      int e;
      for (e = sqlite3_step (stmt_); e == SQLITE_ROW; e = sqlite3_step (stmt_))
        r++;

      if (e != SQLITE_DONE)
        translate_error (e, conn_);

      if (!result_set_)
        r = static_cast<unsigned long long> (
          sqlite3_changes (conn_.handle ()));

      return r;
    }
  }
}