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

#include <cstdlib> // std::exit
#include <iostream>

#ifndef _MSC_VER
#  include <common/config.h>
#endif

#ifdef DB_ID_MYSQL
#  include <odb/mysql/database.hxx>
#  include <odb/mysql/connection-factory.hxx>
#endif

#include <common/common.hxx>
#include <common/options.hxx>

using namespace std;
using namespace odb;

auto_ptr<database>
create_database (int argc, char* argv[], size_t max_connections)
{
#ifdef DB_ID_MYSQL
  cli::argv_file_scanner scan (argc, argv, "--options-file");
  cli::mysql_options ops (scan);

  if (ops.help ())
  {
    cerr << "Usage: " << argv[0] << " [options]" << endl
         << "Options:" << endl;
    cli::mysql_options::print_usage (cerr);
    exit (0);
  }

  auto_ptr<mysql::connection_factory> f;

  if (max_connections != 0)
    f.reset (new mysql::connection_pool_factory (max_connections));

  return auto_ptr<database> (
    new mysql::database (
      ops.user (),
      ops.password_specified () ? &ops.password () : 0,
      ops.database (),
      ops.host (),
      ops.port (),
      ops.socket_specified () ? &ops.socket () : 0,
      0,
      f));
#else
  return auto_ptr<database> (0);
#endif
}