diff options
Diffstat (limited to 'lib/dbshell/client')
-rw-r--r-- | lib/dbshell/client/database.rb | 24 | ||||
-rw-r--r-- | lib/dbshell/client/postgres.rb | 23 | ||||
-rw-r--r-- | lib/dbshell/client/sqlite3.rb | 15 |
3 files changed, 62 insertions, 0 deletions
diff --git a/lib/dbshell/client/database.rb b/lib/dbshell/client/database.rb new file mode 100644 index 0000000..edea777 --- /dev/null +++ b/lib/dbshell/client/database.rb @@ -0,0 +1,24 @@ +require 'dbshell/sqlite3_client' +require 'dbshell/postgres_client' + +class DBShell::DatabaseClient + def self.handler(connection_params) + case connection_params['adapter'] + when 'sqlite3' + DBShell::Sqlite3Client + when 'postgresql' + DBShell::PostgresClient + else + raise DBShell::InvalidDatabaseAdapter + end + end + + def self.runshell(connection_params) + self + .handler(connection_params) + .runshell(connection_params) + end +end + + +class DBShell::InvalidDatabaseAdapter < StandardError; end diff --git a/lib/dbshell/client/postgres.rb b/lib/dbshell/client/postgres.rb new file mode 100644 index 0000000..1c1c663 --- /dev/null +++ b/lib/dbshell/client/postgres.rb @@ -0,0 +1,23 @@ +class DBShell::PostgresClient + EXECUTABLE_NAME = 'psql' + + def self.runshell(db_info) + args = self.build_command(db_info) + exec(*args) + end + + def self.build_command(db_info) + args = [EXECUTABLE_NAME] + + host = db_info['host'] + port = db_info['port'] + db_name = db_info['database'] + user = db_info['username'] + password = db_info['password'] + + args.push('-U', user) if user + args.push('-h', host) if host + args.push('-p', port) if port + args.push(db_name) + end +end diff --git a/lib/dbshell/client/sqlite3.rb b/lib/dbshell/client/sqlite3.rb new file mode 100644 index 0000000..adc71ef --- /dev/null +++ b/lib/dbshell/client/sqlite3.rb @@ -0,0 +1,15 @@ +class DBShell::Sqlite3Client + EXECUTABLE_NAME = 'sqlite3' + + def self.runshell(db_info) + args = self.build_command(db_info) + exec(*args) + end + + def self.build_command(db_info) + args = [ + EXECUTABLE_NAME, + db_info['database'] + ] + end +end |