aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2010-09-14 11:57:40 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2010-09-14 11:57:40 +0200
commit4ac5ce29ecb1a8ce362e411becb9161888bd6eef (patch)
tree8007b3ff9158482e60253f3a8d85f0c149b30de0
parent82aad8dee49a0c645ada631cc8aeb0266ba3ecc5 (diff)
Add argc/argv database ctor
-rw-r--r--build/import/cli/LICENSE21
-rw-r--r--build/import/cli/cli-cxx.make49
-rw-r--r--build/import/cli/configuration-rules.make15
-rwxr-xr-xbuild/import/cli/configure55
-rw-r--r--build/import/cli/stub.make30
-rw-r--r--odb/mysql/connection.cxx2
-rw-r--r--odb/mysql/database.cxx60
-rw-r--r--odb/mysql/database.hxx42
-rw-r--r--odb/mysql/details/options.cli62
-rw-r--r--odb/mysql/exceptions.cxx25
-rw-r--r--odb/mysql/exceptions.hxx12
-rw-r--r--odb/mysql/makefile42
12 files changed, 398 insertions, 17 deletions
diff --git a/build/import/cli/LICENSE b/build/import/cli/LICENSE
new file mode 100644
index 0000000..db16aa3
--- /dev/null
+++ b/build/import/cli/LICENSE
@@ -0,0 +1,21 @@
+Copyright (c) 2009-2010 Code Synthesis Tools CC.
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be included
+in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
diff --git a/build/import/cli/cli-cxx.make b/build/import/cli/cli-cxx.make
new file mode 100644
index 0000000..56dc4b8
--- /dev/null
+++ b/build/import/cli/cli-cxx.make
@@ -0,0 +1,49 @@
+# file : build/import/cli/cli-cxx.make
+# author : Boris Kolpackov <boris@codesynthesis.com>
+# copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
+# license : MIT; see accompanying LICENSE file
+
+# Here we are operating in the importing project's space, not in
+# cli's.
+#
+
+# Get the C++ file extensions.
+#
+$(call include,$(bld_root)/cxx/configuration-static.make)
+
+cli_pattern := \
+$(out_base)/%.$(cxx_s_suffix) \
+$(out_base)/%.$(cxx_h_suffix) \
+$(out_base)/%.$(cxx_i_suffix)
+
+$(cli_pattern): cli_options := \
+--hxx-suffix .$(cxx_h_suffix) \
+--ixx-suffix .$(cxx_i_suffix) \
+--cxx-suffix .$(cxx_s_suffix)
+
+.PRECIOUS: $(cli_pattern)
+
+ifeq ($(out_base),$(src_base))
+
+$(cli_pattern): $(src_base)/%.cli
+ $(call message,cli $<,$(cli) $(cli_options) --output-dir $(dir $@) $<)
+
+else
+
+$(cli_pattern): $(src_base)/%.cli | $$(dir $$@).
+ $(call message,cli $<,$(cli) $(cli_options) --output-dir $(dir $@) $<)
+
+$(cli_pattern): $(out_base)/%.cli | $$(dir $$@).
+ $(call message,cli $<,$(cli) $(cli_options) --output-dir $(dir $@) $<)
+endif
+
+.PHONY: $(out_base)/%.cxx.cli.clean
+
+$(out_base)/%.cxx.cli.clean: cxx_s_suffix := $(cxx_s_suffix)
+$(out_base)/%.cxx.cli.clean: cxx_h_suffix := $(cxx_h_suffix)
+$(out_base)/%.cxx.cli.clean: cxx_i_suffix := $(cxx_i_suffix)
+
+$(out_base)/%.cxx.cli.clean:
+ $(call message,rm $$1,rm -f $$1,$(@:.cxx.cli.clean=.$(cxx_s_suffix)))
+ $(call message,rm $$1,rm -f $$1,$(@:.cxx.cli.clean=.$(cxx_h_suffix)))
+ $(call message,rm $$1,rm -f $$1,$(@:.cxx.cli.clean=.$(cxx_i_suffix)))
diff --git a/build/import/cli/configuration-rules.make b/build/import/cli/configuration-rules.make
new file mode 100644
index 0000000..52c334d
--- /dev/null
+++ b/build/import/cli/configuration-rules.make
@@ -0,0 +1,15 @@
+# file : build/import/cli/configuration-rules.make
+# author : Boris Kolpackov <boris@codesynthesis.com>
+# copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
+# license : MIT; see accompanying LICENSE file
+
+$(dcf_root)/import/cli/configuration-dynamic.make: | $(dcf_root)/import/cli/.
+ $(call message,,$(scf_root)/import/cli/configure $@)
+
+ifndef %foreign%
+
+$(dcf_root)/.disfigure::
+ $(call message,rm $(dcf_root)/import/cli/configuration-dynamic.make,\
+rm -f $(dcf_root)/import/cli/configuration-dynamic.make)
+
+endif
diff --git a/build/import/cli/configure b/build/import/cli/configure
new file mode 100755
index 0000000..21a09c5
--- /dev/null
+++ b/build/import/cli/configure
@@ -0,0 +1,55 @@
+#! /usr/bin/env bash
+
+# file : build/import/cli/configure
+# author : Boris Kolpackov <boris@codesynthesis.com>
+# copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
+# license : MIT; see accompanying LICENSE file
+
+
+# $1 - out file
+#
+# bld_root - build root
+# project_name - project name
+#
+
+source $bld_root/dialog.bash
+
+
+$echo
+$echo "Configuring external dependency on 'cli' for '$project_name'."
+$echo
+
+$echo
+$echo "Would you like to configure dependency on the installed "
+$echo "version of 'cli' as opposed to the development build?"
+$echo
+
+installed=`read_y_n y`
+
+path=
+
+if [ "$installed" = "n" ]; then
+
+$echo
+$echo "Please enter the src_root for 'cli'."
+$echo
+
+src_root=`read_path --directory --exist`
+
+$echo
+$echo "Please enter the out_root for 'cli'."
+$eche
+
+out_root=`read_path --directory $src_root`
+
+fi
+
+echo cli_installed := $installed >$1
+
+if [ "$installed" = "n" ]; then
+
+echo src_root := $src_root >>$1
+echo scf_root := \$\(src_root\)/build >>$1
+echo out_root := $out_root >>$1
+
+fi
diff --git a/build/import/cli/stub.make b/build/import/cli/stub.make
new file mode 100644
index 0000000..250a5ee
--- /dev/null
+++ b/build/import/cli/stub.make
@@ -0,0 +1,30 @@
+# file : build/import/cli/stub.make
+# author : Boris Kolpackov <boris@codesynthesis.com>
+# copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
+# license : MIT; see accompanying LICENSE file
+
+$(call include-once,$(scf_root)/import/cli/configuration-rules.make,$(dcf_root))
+
+cli_installed :=
+
+$(call -include,$(dcf_root)/import/cli/configuration-dynamic.make)
+
+ifdef cli_installed
+
+ifeq ($(cli_installed),y)
+
+$(call export,cli: cli,cli-rules: $(scf_root)/import/cli/cli-cxx.make)
+
+else
+
+# Include export stub.
+#
+$(call include,$(scf_root)/export/cli/stub.make)
+
+endif
+
+else
+
+.NOTPARALLEL:
+
+endif
diff --git a/odb/mysql/connection.cxx b/odb/mysql/connection.cxx
index c6ca5a3..3c17869 100644
--- a/odb/mysql/connection.cxx
+++ b/odb/mysql/connection.cxx
@@ -30,7 +30,7 @@ namespace odb
if (mysql_real_connect (handle_,
db.host (),
db.user (),
- db.passwd (),
+ db.password (),
db.db (),
db.port (),
db.socket (),
diff --git a/odb/mysql/database.cxx b/odb/mysql/database.cxx
index b73e48f..23d6ba2 100644
--- a/odb/mysql/database.cxx
+++ b/odb/mysql/database.cxx
@@ -3,10 +3,15 @@
// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
// license : GNU GPL v2; see accompanying LICENSE file
+#include <sstream>
+
#include <odb/transaction.hxx>
#include <odb/mysql/database.hxx>
#include <odb/mysql/connection-factory.hxx>
+#include <odb/mysql/exceptions.hxx>
+
+#include <odb/mysql/details/options.hxx>
using namespace std;
@@ -149,6 +154,61 @@ namespace odb
factory_->database (*this);
}
+ database::
+ database (int& argc,
+ char* argv[],
+ bool erase,
+ unsigned long client_flags,
+ std::auto_ptr<connection_factory> factory)
+ : passwd_ (0),
+ socket_ (0),
+ client_flags_ (client_flags),
+ factory_ (factory)
+ {
+ using namespace details;
+
+ try
+ {
+ cli::argv_file_scanner scan (argc, argv, "--options-file", erase);
+ options ops (scan);
+
+ user_ = ops.user ();
+
+ if (ops.password_specified ())
+ {
+ passwd_str_ = ops.password ();
+ passwd_ = passwd_str_.c_str ();
+ }
+
+ db_ = ops.database ();
+ host_ = ops.host ();
+ port_ = ops.port ();
+
+ if (ops.socket_specified ())
+ {
+ socket_str_ = ops.socket ();
+ socket_ = socket_str_.c_str ();
+ }
+ }
+ catch (const cli::exception& e)
+ {
+ ostringstream ostr;
+ ostr << e;
+ throw cli_exception (ostr.str ());
+ }
+
+ if (factory_.get () == 0)
+ factory_.reset (new connection_pool_factory ());
+
+ factory_->database (*this);
+ }
+
+ void database::
+ print_usage (std::ostream& os)
+ {
+ details::options::print_usage (os);
+ }
+
transaction_impl* database::
begin_transaction ()
{
diff --git a/odb/mysql/database.hxx b/odb/mysql/database.hxx
index 9f07f03..9072c05 100644
--- a/odb/mysql/database.hxx
+++ b/odb/mysql/database.hxx
@@ -10,6 +10,7 @@
#include <string>
#include <memory> // std::auto_ptr
+#include <iosfwd> // std::ostream
#include <odb/database.hxx>
@@ -34,7 +35,7 @@ namespace odb
typedef mysql::connection connection_type;
public:
- // In MySQL NULL and empty string are treated as the same value
+ // In MySQL, NULL and empty string are treated as the same value
// for all the arguments except passwd and socket.
//
database (const char* user,
@@ -87,6 +88,33 @@ namespace odb
std::auto_ptr<connection_factory> =
std::auto_ptr<connection_factory> (0));
+ // Extract the database parameters from the command line. The
+ // following options are recognized:
+ //
+ // --user
+ // --password
+ // --database
+ // --host
+ // --port
+ // --socket
+ // --options-file
+ //
+ // For more information, see the output of the print_usage() function
+ // below. If erase is true, the above options are removed from the
+ // argv array and the argc is updated accordingly. This constructor
+ // may throw the cli_exception exception.
+ //
+ database (int& argc,
+ char* argv[],
+ bool erase = false,
+ unsigned long client_flags = 0,
+ std::auto_ptr<connection_factory> =
+ std::auto_ptr<connection_factory> (0));
+
+ static void
+ print_usage (std::ostream&);
+
+
public:
const char*
user () const
@@ -95,7 +123,7 @@ namespace odb
}
const char*
- passwd () const
+ password () const
{
return passwd_;
}
@@ -143,13 +171,13 @@ namespace odb
~database ();
private:
- const std::string user_;
- const std::string passwd_str_;
+ std::string user_;
+ std::string passwd_str_;
const char* passwd_;
- const std::string db_;
- const std::string host_;
+ std::string db_;
+ std::string host_;
unsigned int port_;
- const std::string socket_str_;
+ std::string socket_str_;
const char* socket_;
unsigned long client_flags_;
std::auto_ptr<connection_factory> factory_;
diff --git a/odb/mysql/details/options.cli b/odb/mysql/details/options.cli
new file mode 100644
index 0000000..04de6b0
--- /dev/null
+++ b/odb/mysql/details/options.cli
@@ -0,0 +1,62 @@
+// file : odb/mysql/details/options.cli
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+include <string>;
+
+namespace odb
+{
+ namespace mysql
+ {
+ namespace details
+ {
+ class options
+ {
+ std::string --user
+ {
+ "<name>",
+ "MySQL database user."
+ };
+
+ std::string --password
+ {
+ "<str>",
+ "MySQL database password"
+ };
+
+ std::string --database
+ {
+ "<name>",
+ "MySQL database name."
+ };
+
+ std::string --host
+ {
+ "<addr>",
+ "MySQL database host name or address (localhost by default)."
+ };
+
+ unsigned int --port = 0
+ {
+ "<integer>",
+ "MySQL database port number."
+ };
+
+ std::string --socket
+ {
+ "<name>",
+ "MySQL database socket name."
+ };
+
+ std::string --options-file
+ {
+ "<file>",
+ "Read additional options from <file>. Each option appearing on a
+ separate line optionally followed by space and an option value.
+ Empty lines and lines starting with \cb{#} are ignored."
+ };
+ };
+ }
+ }
+}
diff --git a/odb/mysql/exceptions.cxx b/odb/mysql/exceptions.cxx
index 1c535dd..3446651 100644
--- a/odb/mysql/exceptions.cxx
+++ b/odb/mysql/exceptions.cxx
@@ -15,6 +15,10 @@ namespace odb
{
namespace mysql
{
+ //
+ // database_exception
+ //
+
database_exception::
~database_exception () throw ()
{
@@ -72,5 +76,26 @@ namespace odb
{
return what_.c_str ();
}
+
+ //
+ // cli_exception
+ //
+
+ cli_exception::
+ cli_exception (const std::string& what)
+ : what_ (what)
+ {
+ }
+
+ cli_exception::
+ ~cli_exception () throw ()
+ {
+ }
+
+ const char* cli_exception::
+ what () const throw ()
+ {
+ return what_.c_str ();
+ }
}
}
diff --git a/odb/mysql/exceptions.hxx b/odb/mysql/exceptions.hxx
index f9d3730..3902236 100644
--- a/odb/mysql/exceptions.hxx
+++ b/odb/mysql/exceptions.hxx
@@ -62,6 +62,18 @@ namespace odb
std::string message_;
std::string what_;
};
+
+ struct LIBODB_MYSQL_EXPORT cli_exception: odb::exception
+ {
+ cli_exception (const std::string& what);
+ ~cli_exception () throw ();
+
+ virtual const char*
+ what () const throw ();
+
+ private:;
+ std::string what_;
+ };
}
}
diff --git a/odb/mysql/makefile b/odb/mysql/makefile
index 8fe0c24..8bec46b 100644
--- a/odb/mysql/makefile
+++ b/odb/mysql/makefile
@@ -16,8 +16,9 @@ statement.cxx \
query.cxx \
traits.cxx
+cli_tun := details/options.cli
cxx_tun := $(cxx)
-cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o))
+cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(cli_tun:.cli=.o))
cxx_od := $(cxx_obj:.o=.o.d)
odb_mysql.l := $(out_base)/odb-mysql.l
@@ -30,6 +31,10 @@ clean := $(out_base)/.clean
# Import.
#
$(call import,\
+ $(scf_root)/import/cli/stub.make,\
+ cli: cli,cli-rules: cli_rules)
+
+$(call import,\
$(scf_root)/import/libodb/stub.make,\
l: odb.l,cpp-options: odb.l.cpp-options)
@@ -49,7 +54,16 @@ $(odb_mysql.l.cpp-options): $(odb.l.cpp-options) $(mysql.l.cpp-options)
$(cxx_obj) $(cxx_od): $(odb_mysql.l.cpp-options) $(out_base)/details/config.h
-$(call include-dep,$(cxx_od))
+genf := $(cli_tun:.cli=.hxx) $(cli_tun:.cli=.ixx) $(cli_tun:.cli=.cxx)
+gen := $(addprefix $(out_base)/,$(genf))
+
+$(gen): $(cli)
+$(gen): cli := $(cli)
+$(gen): cli_options += \
+--generate-specifier \
+--generate-file-scanner \
+--cli-namespace odb::mysql::details::cli \
+--guard-prefix LIBODB_MYSQL_DETAILS
$(out_base)/details/config.h:
@echo '// file : odb/mysql/details/config.h' >$@
@@ -62,22 +76,30 @@ $(out_base)/details/config.h:
@echo '' >>$@
@echo '#endif // ODB_MYSQL_DETAILS_CONFIG_H' >>$@
+$(call include-dep,$(cxx_od),$(cxx_obj),$(gen) $(out_base)/details/config.h)
+
# Convenience alias for default target.
#
$(out_base)/: $(odb_mysql.l)
# Dist.
#
-$(dist): export sources := $(cxx)
-$(dist): export headers = $(subst $(src_base)/,,$(shell find $(src_base) \
+$(dist): sources_dist := $(cxx)
+$(dist): export sources := $(sources_dist) $(cli_tun:.cli=.cxx)
+$(dist): headers_dist = $(subst $(src_base)/,,$(shell find $(src_base) \
-name '*.hxx' -o -name '*.ixx' -o -name '*.txx'))
-$(dist): export extra_dist := libodb-mysql-vc9.vcproj \
+$(dist): gen_headers := $(cli_tun:.cli=.hxx) $(cli_tun:.cli=.ixx)
+$(dist): export headers = $(sort $(headers_dist) $(gen_headers))
+$(dist): gen_dist := $(gen)
+$(dist): data_dist := $(cli_tun) details/config.h.in
+$(dist): export extra_dist := $(data_dist) libodb-mysql-vc9.vcproj \
libodb-mysql-vc10.vcxproj libodb-mysql-vc10.vcxproj.filters
$(dist): export interface_version = $(shell sed -e \
's/^\([0-9]*\.[0-9]*\).*/\1/' $(src_root)/version)
-$(dist):
- $(call dist-data,$(sources) $(headers) details/config.h.in)
+$(dist): $(gen)
+ $(call dist-data,$(sources_dist) $(headers_dist) $(data_dist))
+ $(call dist-data,$(gen_dist),$(dist_prefix)/odb/mysql/details)
$(call meta-vc9proj,$(src_base)/libodb-mysql-vc9.vcproj)
$(call meta-vc10proj,$(src_base)/libodb-mysql-vc10.vcxproj)
$(call meta-automake)
@@ -87,7 +109,8 @@ $(dist):
$(clean): $(odb_mysql.l).o.clean \
$(odb_mysql.l.cpp-options).clean \
$(addsuffix .cxx.clean,$(cxx_obj)) \
- $(addsuffix .cxx.clean,$(cxx_od))
+ $(addsuffix .cxx.clean,$(cxx_od)) \
+ $(addprefix $(out_base)/,$(cli_tun:.cli=.cxx.cli.clean))
$(call message,rm $$1,rm -f $$1,$(out_base)/details/config.h)
# Generated .gitignore.
@@ -95,7 +118,7 @@ $(clean): $(odb_mysql.l).o.clean \
ifeq ($(out_base),$(src_base))
$(odb_mysql.l): | $(out_base)/.gitignore
-$(out_base)/.gitignore: files := details/config.h
+$(out_base)/.gitignore: files := details/config.h $(genf)
$(clean): $(out_base)/.gitignore.clean
$(call include,$(bld_root)/git/gitignore.make)
@@ -108,6 +131,7 @@ $(call include,$(bld_root)/meta/vc9proj.make)
$(call include,$(bld_root)/meta/vc10proj.make)
$(call include,$(bld_root)/meta/automake.make)
+$(call include,$(cli_rules))
$(call include,$(bld_root)/cxx/o-l.make)
$(call include,$(bld_root)/cxx/cxx-o.make)
$(call include,$(bld_root)/cxx/cxx-d.make)