dbagile 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENCE.textile +12 -0
- data/README.textile +89 -0
- data/bin/dba +22 -0
- data/lib/dbagile/adapter/sequel/class_methods.rb +18 -0
- data/lib/dbagile/adapter/sequel/connection.rb +38 -0
- data/lib/dbagile/adapter/sequel/data/table_driven.rb +30 -0
- data/lib/dbagile/adapter/sequel/data/transaction_driven.rb +43 -0
- data/lib/dbagile/adapter/sequel/schema/concrete_script.rb +135 -0
- data/lib/dbagile/adapter/sequel/schema/physical_dump.rb +106 -0
- data/lib/dbagile/adapter/sequel/schema/schema2sequel_args.rb +71 -0
- data/lib/dbagile/adapter/sequel/schema/table_driven.rb +52 -0
- data/lib/dbagile/adapter/sequel/schema/transaction_driven.rb +46 -0
- data/lib/dbagile/adapter/sequel/sequel_tracer.rb +144 -0
- data/lib/dbagile/adapter/sequel.rb +46 -0
- data/lib/dbagile/adapter.rb +15 -0
- data/lib/dbagile/command/api.rb +49 -0
- data/lib/dbagile/command/bulk/commons.rb +130 -0
- data/lib/dbagile/command/bulk/export.rb +99 -0
- data/lib/dbagile/command/bulk/import.rb +147 -0
- data/lib/dbagile/command/bulk.rb +3 -0
- data/lib/dbagile/command/class_methods.rb +103 -0
- data/lib/dbagile/command/db/add.rb +94 -0
- data/lib/dbagile/command/db/list.rb +40 -0
- data/lib/dbagile/command/db/ping.rb +49 -0
- data/lib/dbagile/command/db/rm.rb +52 -0
- data/lib/dbagile/command/db/stage.rb +81 -0
- data/lib/dbagile/command/db/use.rb +48 -0
- data/lib/dbagile/command/db.rb +6 -0
- data/lib/dbagile/command/dba.rb +121 -0
- data/lib/dbagile/command/help.rb +50 -0
- data/lib/dbagile/command/repo/create.rb +54 -0
- data/lib/dbagile/command/repo.rb +1 -0
- data/lib/dbagile/command/robust.rb +86 -0
- data/lib/dbagile/command/schema/check.rb +59 -0
- data/lib/dbagile/command/schema/commons.rb +118 -0
- data/lib/dbagile/command/schema/diff.rb +101 -0
- data/lib/dbagile/command/schema/dump.rb +48 -0
- data/lib/dbagile/command/schema/merge.rb +55 -0
- data/lib/dbagile/command/schema/sql_script.rb +124 -0
- data/lib/dbagile/command/schema.rb +6 -0
- data/lib/dbagile/command/sql/drop.rb +40 -0
- data/lib/dbagile/command/sql/heading.rb +34 -0
- data/lib/dbagile/command/sql/send.rb +67 -0
- data/lib/dbagile/command/sql/show.rb +42 -0
- data/lib/dbagile/command/sql.rb +4 -0
- data/lib/dbagile/command/web/tools.rb +23 -0
- data/lib/dbagile/command/web.rb +1 -0
- data/lib/dbagile/command.rb +158 -0
- data/lib/dbagile/contract/connection.rb +66 -0
- data/lib/dbagile/contract/data/dataset.rb +69 -0
- data/lib/dbagile/contract/data/table_driven.rb +49 -0
- data/lib/dbagile/contract/data/transaction_driven.rb +74 -0
- data/lib/dbagile/contract/data.rb +3 -0
- data/lib/dbagile/contract/robust/helpers.rb +19 -0
- data/lib/dbagile/contract/robust/optimistic/data/table_driven.rb +31 -0
- data/lib/dbagile/contract/robust/optimistic/data/transaction_driven.rb +45 -0
- data/lib/dbagile/contract/robust/optimistic/schema/table_driven.rb +53 -0
- data/lib/dbagile/contract/robust/optimistic/schema/transaction_driven.rb +45 -0
- data/lib/dbagile/contract/robust/optimistic.rb +18 -0
- data/lib/dbagile/contract/robust.rb +20 -0
- data/lib/dbagile/contract/schema/table_driven.rb +89 -0
- data/lib/dbagile/contract/schema/transaction_driven.rb +68 -0
- data/lib/dbagile/contract/schema.rb +2 -0
- data/lib/dbagile/contract/utils/delegate.rb +17 -0
- data/lib/dbagile/contract/utils/full.rb +13 -0
- data/lib/dbagile/contract/utils.rb +2 -0
- data/lib/dbagile/contract.rb +5 -0
- data/lib/dbagile/core/chain.rb +92 -0
- data/lib/dbagile/core/connection.rb +51 -0
- data/lib/dbagile/core/database.rb +221 -0
- data/lib/dbagile/core/io/dsl.rb +95 -0
- data/lib/dbagile/core/io/robustness.rb +82 -0
- data/lib/dbagile/core/io.rb +2 -0
- data/lib/dbagile/core/repository/builder.rb +67 -0
- data/lib/dbagile/core/repository/yaml_methods.rb +82 -0
- data/lib/dbagile/core/repository.rb +211 -0
- data/lib/dbagile/core/schema/builder/coercion.rb +210 -0
- data/lib/dbagile/core/schema/builder/concept_factory.rb +61 -0
- data/lib/dbagile/core/schema/builder.rb +187 -0
- data/lib/dbagile/core/schema/composite.rb +239 -0
- data/lib/dbagile/core/schema/computations/filter.rb +40 -0
- data/lib/dbagile/core/schema/computations/merge.rb +55 -0
- data/lib/dbagile/core/schema/computations/minus.rb +44 -0
- data/lib/dbagile/core/schema/computations/split.rb +37 -0
- data/lib/dbagile/core/schema/computations.rb +4 -0
- data/lib/dbagile/core/schema/database_schema.rb +129 -0
- data/lib/dbagile/core/schema/errors.rb +173 -0
- data/lib/dbagile/core/schema/logical/attribute.rb +68 -0
- data/lib/dbagile/core/schema/logical/constraint/candidate_key.rb +70 -0
- data/lib/dbagile/core/schema/logical/constraint/foreign_key.rb +121 -0
- data/lib/dbagile/core/schema/logical/constraint.rb +58 -0
- data/lib/dbagile/core/schema/logical/constraints.rb +28 -0
- data/lib/dbagile/core/schema/logical/heading.rb +47 -0
- data/lib/dbagile/core/schema/logical/relvar.rb +81 -0
- data/lib/dbagile/core/schema/logical.rb +24 -0
- data/lib/dbagile/core/schema/migrate/abstract_script.rb +35 -0
- data/lib/dbagile/core/schema/migrate/collapse_table.rb +15 -0
- data/lib/dbagile/core/schema/migrate/create_table.rb +15 -0
- data/lib/dbagile/core/schema/migrate/drop_table.rb +15 -0
- data/lib/dbagile/core/schema/migrate/expand_table.rb +15 -0
- data/lib/dbagile/core/schema/migrate/operation.rb +141 -0
- data/lib/dbagile/core/schema/migrate/stager.rb +282 -0
- data/lib/dbagile/core/schema/migrate.rb +2 -0
- data/lib/dbagile/core/schema/part.rb +114 -0
- data/lib/dbagile/core/schema/physical/index.rb +64 -0
- data/lib/dbagile/core/schema/physical/indexes.rb +12 -0
- data/lib/dbagile/core/schema/physical.rb +26 -0
- data/lib/dbagile/core/schema/robustness.rb +39 -0
- data/lib/dbagile/core/schema/schema_object.rb +94 -0
- data/lib/dbagile/core/schema.rb +254 -0
- data/lib/dbagile/core/transaction.rb +74 -0
- data/lib/dbagile/core.rb +7 -0
- data/lib/dbagile/environment/buffering.rb +45 -0
- data/lib/dbagile/environment/delegator.rb +59 -0
- data/lib/dbagile/environment/interactions.rb +208 -0
- data/lib/dbagile/environment/on_error.rb +47 -0
- data/lib/dbagile/environment/repository.rb +161 -0
- data/lib/dbagile/environment/robustness.rb +7 -0
- data/lib/dbagile/environment/testing.rb +23 -0
- data/lib/dbagile/environment.rb +122 -0
- data/lib/dbagile/errors.rb +30 -0
- data/lib/dbagile/io/csv.rb +99 -0
- data/lib/dbagile/io/json.rb +41 -0
- data/lib/dbagile/io/pretty_table.rb +128 -0
- data/lib/dbagile/io/ruby.rb +62 -0
- data/lib/dbagile/io/text.rb +18 -0
- data/lib/dbagile/io/type_safe.rb +65 -0
- data/lib/dbagile/io/xml.rb +35 -0
- data/lib/dbagile/io/yaml.rb +30 -0
- data/lib/dbagile/io.rb +94 -0
- data/lib/dbagile/loader.rb +16 -0
- data/lib/dbagile/plugin.rb +29 -0
- data/lib/dbagile/restful/client/delete.rb +22 -0
- data/lib/dbagile/restful/client/get.rb +24 -0
- data/lib/dbagile/restful/client/post.rb +22 -0
- data/lib/dbagile/restful/client/utils.rb +16 -0
- data/lib/dbagile/restful/client.rb +41 -0
- data/lib/dbagile/restful/middleware/delete.rb +22 -0
- data/lib/dbagile/restful/middleware/get.rb +27 -0
- data/lib/dbagile/restful/middleware/one_database.rb +82 -0
- data/lib/dbagile/restful/middleware/post.rb +23 -0
- data/lib/dbagile/restful/middleware/utils.rb +65 -0
- data/lib/dbagile/restful/middleware.rb +54 -0
- data/lib/dbagile/restful/server.rb +65 -0
- data/lib/dbagile/restful.rb +9 -0
- data/lib/dbagile/robustness/dependencies.rb +36 -0
- data/lib/dbagile/robustness/file_system.rb +53 -0
- data/lib/dbagile/robustness.rb +14 -0
- data/lib/dbagile/tools/file_system.rb +24 -0
- data/lib/dbagile/tools/math.rb +11 -0
- data/lib/dbagile/tools/ordered_hash.rb +39 -0
- data/lib/dbagile/tools/ruby.rb +78 -0
- data/lib/dbagile/tools/string.rb +6 -0
- data/lib/dbagile/tools/tuple.rb +49 -0
- data/lib/dbagile/tools.rb +6 -0
- data/lib/dbagile.rb +66 -0
- data/test/assumptions/equality.spec +11 -0
- data/test/assumptions/fixtures.rb +39 -0
- data/test/assumptions/inheritance.spec +17 -0
- data/test/assumptions/sequel/autonumber.spec +19 -0
- data/test/assumptions/sequel/connect.spec +29 -0
- data/test/assumptions/sequel/test.db +0 -0
- data/test/assumptions/stdlib/pathname.spec +13 -0
- data/test/assumptions/yaml/fixtures.rb +25 -0
- data/test/assumptions/yaml/to_yaml.spec +10 -0
- data/test/assumptions.spec +2 -0
- data/test/commands/bulk/export.spec +100 -0
- data/test/commands/bulk/import.spec +49 -0
- data/test/commands/db/add.spec +31 -0
- data/test/commands/db/list.spec +29 -0
- data/test/commands/db/ping.spec +21 -0
- data/test/commands/db/rm.spec +18 -0
- data/test/commands/db/use.spec +18 -0
- data/test/commands/dba.spec +54 -0
- data/test/commands/repo/create.spec +30 -0
- data/test/commands/schema/check.spec +25 -0
- data/test/commands/schema/diff.spec +30 -0
- data/test/commands/schema/dump.spec +23 -0
- data/test/commands/schema/fixtures/add_constraint.yaml +30 -0
- data/test/commands/schema/fixtures/announced.yaml +28 -0
- data/test/commands/schema/fixtures/effective.yaml +20 -0
- data/test/commands/schema/fixtures/invalid.yaml +6 -0
- data/test/commands/schema/sql_script.spec +56 -0
- data/test/commands/sql/drop.spec +25 -0
- data/test/commands/sql/heading.spec +7 -0
- data/test/commands/sql/scripts/delete.sql +1 -0
- data/test/commands/sql/scripts/insert.sql +2 -0
- data/test/commands/sql/send.spec +29 -0
- data/test/commands/sql/show.spec +17 -0
- data/test/commands.spec +138 -0
- data/test/contract/connection/transaction.ex +11 -0
- data/test/contract/connection.spec +9 -0
- data/test/contract/data/dataset/columns.ex +5 -0
- data/test/contract/data/dataset/count.ex +5 -0
- data/test/contract/data/dataset.spec +9 -0
- data/test/contract/data/table_driven/dataset.ex +31 -0
- data/test/contract/data/table_driven/exists_q.ex +27 -0
- data/test/contract/data/table_driven.spec +9 -0
- data/test/contract/data/transaction_driven/delete.ex +29 -0
- data/test/contract/data/transaction_driven/direct_sql.ex +19 -0
- data/test/contract/data/transaction_driven/insert.ex +8 -0
- data/test/contract/data/transaction_driven/update.ex +19 -0
- data/test/contract/data/transaction_driven.spec +18 -0
- data/test/contract/robust/data/table_driven.spec +15 -0
- data/test/contract/robust/data/transaction_driven.spec +21 -0
- data/test/contract/robust/schema/table_driven.spec +21 -0
- data/test/contract/robust/schema/transaction_driven.spec +19 -0
- data/test/contract/schema/table_driven/column_names.ex +5 -0
- data/test/contract/schema/table_driven/has_column_q.ex +13 -0
- data/test/contract/schema/table_driven/has_table_q.ex +11 -0
- data/test/contract/schema/table_driven/heading.ex +5 -0
- data/test/contract/schema/table_driven.spec +9 -0
- data/test/contract/schema/transaction_driven/create_table.ex +10 -0
- data/test/contract/schema/transaction_driven/drop_table.ex +10 -0
- data/test/contract/schema/transaction_driven.spec +18 -0
- data/test/contract.spec +66 -0
- data/test/fixtures/basics/data/basic_values.rb +13 -0
- data/test/fixtures/basics/data/empty_table.rb +3 -0
- data/test/fixtures/basics/data/non_empty_table.rb +4 -0
- data/test/fixtures/basics/data/parts.rb +8 -0
- data/test/fixtures/basics/data/suppliers.rb +7 -0
- data/test/fixtures/basics/data/supplies.rb +14 -0
- data/test/fixtures/basics/dbagile.idx +20 -0
- data/test/fixtures/basics/fixtures.yaml +28 -0
- data/test/fixtures/basics/robust.db +0 -0
- data/test/fixtures/basics/suppliers.yaml +30 -0
- data/test/fixtures/basics/test.db +0 -0
- data/test/fixtures/empty/dbagile.idx +5 -0
- data/test/fixtures.rb +152 -0
- data/test/restful/delete/no_format.ex +32 -0
- data/test/restful/delete.spec +8 -0
- data/test/restful/get/csv_format.ex +12 -0
- data/test/restful/get/json_format.ex +19 -0
- data/test/restful/get/query_string.ex +11 -0
- data/test/restful/get/text_format.ex +12 -0
- data/test/restful/get/yaml_format.ex +14 -0
- data/test/restful/get.spec +5 -0
- data/test/restful/post/no_format.ex +22 -0
- data/test/restful/post.spec +8 -0
- data/test/restful.spec +32 -0
- data/test/run_all_suite.rb +51 -0
- data/test/spec_helper.rb +26 -0
- data/test/support/be_a_valid_json_string.rb +19 -0
- data/test/support/be_a_valid_yaml_string.rb +18 -0
- data/test/unit/adapter/factor.spec +13 -0
- data/test/unit/adapter/sequel/new.spec +19 -0
- data/test/unit/command/api.spec +12 -0
- data/test/unit/command/command_for.spec +36 -0
- data/test/unit/command/command_name_of.spec +21 -0
- data/test/unit/command/ruby_method_for.spec +21 -0
- data/test/unit/command/sanity.spec +34 -0
- data/test/unit/contract/utils/delegate/delegate.spec +23 -0
- data/test/unit/core/chain/chain.spec +57 -0
- data/test/unit/core/chain/connect.spec +22 -0
- data/test/unit/core/chain/delegate_chain.spec +16 -0
- data/test/unit/core/chain/initialize.spec +19 -0
- data/test/unit/core/chain/plug.spec +31 -0
- data/test/unit/core/io/dsl/scope.spec +9 -0
- data/test/unit/core/repository/create_bang.spec +31 -0
- data/test/unit/core/repository/current.spec +31 -0
- data/test/unit/core/repository/database.spec +47 -0
- data/test/unit/core/repository/fixtures/corrupted/dbagile.idx +1 -0
- data/test/unit/core/repository/fixtures/test_and_prod/dbagile.idx +21 -0
- data/test/unit/core/repository/fixtures.rb +25 -0
- data/test/unit/core/repository/has_database_q.spec +16 -0
- data/test/unit/core/repository/load.spec +51 -0
- data/test/unit/core/repository/to_yaml.spec +17 -0
- data/test/unit/core/schema/check.spec +32 -0
- data/test/unit/core/schema/empty_q.spec +18 -0
- data/test/unit/core/schema/filter.spec +42 -0
- data/test/unit/core/schema/fixtures/dbagile.yaml +7 -0
- data/test/unit/core/schema/fixtures/empty.yaml +11 -0
- data/test/unit/core/schema/fixtures/invalid.yaml +54 -0
- data/test/unit/core/schema/fixtures/left.yaml +46 -0
- data/test/unit/core/schema/fixtures/left_minus_right.yaml +31 -0
- data/test/unit/core/schema/fixtures/right.yaml +46 -0
- data/test/unit/core/schema/fixtures/right_minus_left.yaml +31 -0
- data/test/unit/core/schema/fixtures/suppliers_and_parts.yaml +30 -0
- data/test/unit/core/schema/fixtures.rb +32 -0
- data/test/unit/core/schema/merge.spec +72 -0
- data/test/unit/core/schema/minus.spec +26 -0
- data/test/unit/core/schema/sanity.spec +39 -0
- data/test/unit/core/schema/split.spec +58 -0
- data/test/unit/core/schema/stage_script.spec +26 -0
- data/test/unit/core/schema/to_yaml.spec +13 -0
- data/test/unit/core/schema/yaml_display.spec +14 -0
- data/test/unit/core/schema/yaml_load.spec +20 -0
- data/test/unit/core/transaction/transaction.spec +10 -0
- data/test/unit/fixtures.rb +67 -0
- data/test/unit/io/to_xxx.spec +52 -0
- data/test/unit/plugin/options.spec +21 -0
- data/test/unit/plugin/tuple_heading.spec +11 -0
- data/test/unit/plugin/with_options.spec +12 -0
- data/test/unit/tools/ruby/class_unqualified_name.spec +26 -0
- data/test/unit/tools/ruby/extract_file_rdoc.spec +10 -0
- data/test/unit/tools/ruby/fixtures/rdoc.txt +12 -0
- data/test/unit/tools/ruby/fixtures.rb +19 -0
- data/test/unit/tools/ruby/optional_args_block_call.spec +35 -0
- data/test/unit/tools/ruby/parent_module.spec +21 -0
- data/test/unit/tools/ruby/rdoc_file_paragraphs.spec +13 -0
- data/test/unit/tools/tuple/tuple_heading.spec +11 -0
- data/test/unit/tools/tuple/tuple_key.spec +27 -0
- data/test/unit/tools/tuple/tuple_project.spec +23 -0
- data/test/unit.spec +3 -0
- metadata +422 -0
@@ -0,0 +1,67 @@
|
|
1
|
+
module DbAgile
|
2
|
+
class Command
|
3
|
+
module SQL
|
4
|
+
#
|
5
|
+
# Send SQL commands directly to the DBMS
|
6
|
+
#
|
7
|
+
# Usage: dba #{command_name} [--file=SCRIPT] [QUERY]
|
8
|
+
#
|
9
|
+
class Send < Command
|
10
|
+
Command::build_me(self, __FILE__)
|
11
|
+
|
12
|
+
# Query to send
|
13
|
+
attr_accessor :query
|
14
|
+
|
15
|
+
# The file to execute
|
16
|
+
attr_accessor :file
|
17
|
+
|
18
|
+
# Contribute to options
|
19
|
+
def add_options(opt)
|
20
|
+
opt.separator nil
|
21
|
+
opt.separator "Options:"
|
22
|
+
opt.on("--file=SCRIPT", '-f',
|
23
|
+
"Executes a whole SQL script file") do |value|
|
24
|
+
self.file = valid_read_file!(value)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Normalizes the pending arguments
|
29
|
+
def normalize_pending_arguments(arguments)
|
30
|
+
case arguments.size
|
31
|
+
when 0
|
32
|
+
when 1
|
33
|
+
self.query = valid_argument_list!(arguments, String)
|
34
|
+
else
|
35
|
+
bad_argument_list!(arguments)
|
36
|
+
end
|
37
|
+
ambigous_argument_list! if self.query and self.file
|
38
|
+
end
|
39
|
+
|
40
|
+
# Executes the command.
|
41
|
+
def execute_command
|
42
|
+
result = nil
|
43
|
+
with_current_connection do |connection|
|
44
|
+
connection.transaction do |t|
|
45
|
+
if self.file
|
46
|
+
result = t.direct_sql(File.read(self.file))
|
47
|
+
elsif self.query
|
48
|
+
result = t.direct_sql(self.query)
|
49
|
+
else
|
50
|
+
script = environment.input_buffer.readlines.join("\n")
|
51
|
+
result = t.direct_sql(script)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
case result
|
56
|
+
when DbAgile::Contract::Data::Dataset
|
57
|
+
result.to_text(environment.output_buffer)
|
58
|
+
else
|
59
|
+
flush(result)
|
60
|
+
end
|
61
|
+
result
|
62
|
+
end
|
63
|
+
|
64
|
+
end # class Send
|
65
|
+
end # module SQL
|
66
|
+
end # class Command
|
67
|
+
end # module DbAgile
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module DbAgile
|
2
|
+
class Command
|
3
|
+
module SQL
|
4
|
+
#
|
5
|
+
# Display content of a table/view/query (shortcut for 'export --text ...')
|
6
|
+
#
|
7
|
+
# Usage: dba #{command_name} [OPTIONS] DATASET
|
8
|
+
#
|
9
|
+
class Show < Command
|
10
|
+
Command::build_me(self, __FILE__)
|
11
|
+
|
12
|
+
# Contribute to options
|
13
|
+
def add_options(opt)
|
14
|
+
opt.separator nil
|
15
|
+
opt.separator "Options:"
|
16
|
+
opt.on("--[no-]pretty",
|
17
|
+
"Make/Avoid pretty output (truncate string at terminal size and so on.)") do |value|
|
18
|
+
self.pretty = value
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Infer options
|
23
|
+
def infer_options(argv)
|
24
|
+
argv = ["--text"] + argv
|
25
|
+
if argv.include?("--no-pretty")
|
26
|
+
argv.delete("--no-pretty")
|
27
|
+
else
|
28
|
+
argv += ["--truncate-at", environment.console_width.to_s]
|
29
|
+
end
|
30
|
+
argv
|
31
|
+
end
|
32
|
+
|
33
|
+
# Override to avoid pending options to be rejected
|
34
|
+
def unsecure_run(requester_file, argv)
|
35
|
+
DbAgile::dba(environment){|dba| dba.bulk_export(infer_options(argv))}
|
36
|
+
environment.output_buffer
|
37
|
+
end
|
38
|
+
|
39
|
+
end # class Show
|
40
|
+
end # module SQL
|
41
|
+
end # class Command
|
42
|
+
end # module DbAgile
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module DbAgile
|
2
|
+
class Command
|
3
|
+
module Web
|
4
|
+
#
|
5
|
+
# Start the web tools
|
6
|
+
#
|
7
|
+
# Usage: dba #{command_name} [OPTIONS]
|
8
|
+
#
|
9
|
+
class Tools < Command
|
10
|
+
Command::build_me(self, __FILE__)
|
11
|
+
|
12
|
+
# Executes the command.
|
13
|
+
def execute_command
|
14
|
+
require 'dbagile/restful/server'
|
15
|
+
DbAgile::Restful::Server.new(environment).start.join
|
16
|
+
rescue Interrupt => ex
|
17
|
+
environment.say("Ciao!")
|
18
|
+
end
|
19
|
+
|
20
|
+
end # class Tools
|
21
|
+
end # module Web
|
22
|
+
end # class Command
|
23
|
+
end # module DbAgile
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'dbagile/command/web/tools'
|
@@ -0,0 +1,158 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
require 'fileutils'
|
3
|
+
require 'dbagile/command/robust'
|
4
|
+
require 'dbagile/command/class_methods'
|
5
|
+
module DbAgile
|
6
|
+
class Command
|
7
|
+
include ::DbAgile::Command::Robust
|
8
|
+
include ::DbAgile::Environment::Delegator
|
9
|
+
extend ::DbAgile::Command::ClassMethods
|
10
|
+
|
11
|
+
# Command categories
|
12
|
+
CATEGORIES = [:dba, :repo, :db, :sql, :schema, :bulk, :web]
|
13
|
+
|
14
|
+
# Names of the categories
|
15
|
+
CATEGORY_NAMES = {
|
16
|
+
:dba => "Main commands:",
|
17
|
+
:repo => "Repository management:",
|
18
|
+
:db => "Database management:",
|
19
|
+
:bulk => "Import/Export management:",
|
20
|
+
:sql => "SQL:",
|
21
|
+
:schema => "Database schema:",
|
22
|
+
:web => "Database and the web:"
|
23
|
+
}
|
24
|
+
|
25
|
+
##############################################################################
|
26
|
+
### Instance variables and construction
|
27
|
+
##############################################################################
|
28
|
+
|
29
|
+
# Command execution environment
|
30
|
+
attr_reader :environment
|
31
|
+
|
32
|
+
# Creates an empty command instance
|
33
|
+
def initialize(env)
|
34
|
+
@environment = env
|
35
|
+
set_default_options
|
36
|
+
end
|
37
|
+
|
38
|
+
# Returns commands category
|
39
|
+
def category
|
40
|
+
self.class.category
|
41
|
+
end
|
42
|
+
|
43
|
+
# Returns command name
|
44
|
+
def command_name
|
45
|
+
self.class.command_name
|
46
|
+
end
|
47
|
+
|
48
|
+
# Returns command summary
|
49
|
+
def summary
|
50
|
+
self.class.summary || ""
|
51
|
+
end
|
52
|
+
|
53
|
+
# Returns command summary
|
54
|
+
def usage
|
55
|
+
self.class.usage || ""
|
56
|
+
end
|
57
|
+
alias :banner :usage
|
58
|
+
|
59
|
+
def description
|
60
|
+
self.class.description || ""
|
61
|
+
end
|
62
|
+
|
63
|
+
##############################################################################
|
64
|
+
### About options
|
65
|
+
##############################################################################
|
66
|
+
|
67
|
+
# Parses commandline options provided as an array of Strings.
|
68
|
+
def options
|
69
|
+
@options ||= OptionParser.new do |opt|
|
70
|
+
opt.program_name = File.basename $0
|
71
|
+
opt.version = DbAgile::VERSION
|
72
|
+
opt.release = nil
|
73
|
+
opt.summary_indent = ' ' * 2
|
74
|
+
opt.banner = self.banner
|
75
|
+
add_options(opt)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# Sets the default options
|
80
|
+
def set_default_options
|
81
|
+
end
|
82
|
+
|
83
|
+
# Contribute to options
|
84
|
+
def add_options(opt)
|
85
|
+
end
|
86
|
+
|
87
|
+
##############################################################################
|
88
|
+
### Command info/help and so on.
|
89
|
+
##############################################################################
|
90
|
+
|
91
|
+
# Shows the help
|
92
|
+
def show_help
|
93
|
+
display banner
|
94
|
+
display ""
|
95
|
+
display short_help
|
96
|
+
display ""
|
97
|
+
end
|
98
|
+
|
99
|
+
##############################################################################
|
100
|
+
### Run logic
|
101
|
+
##############################################################################
|
102
|
+
|
103
|
+
# Runs the command
|
104
|
+
def run(requester_file, argv)
|
105
|
+
unsecure_run(requester_file, argv)
|
106
|
+
rescue Exception => ex
|
107
|
+
environment.on_error(self, ex)
|
108
|
+
environment
|
109
|
+
end
|
110
|
+
|
111
|
+
# Runs the command without catching any error
|
112
|
+
def unsecure_run(requester_file, argv)
|
113
|
+
rest = options.parse!(argv)
|
114
|
+
normalize_pending_arguments(rest)
|
115
|
+
check_command
|
116
|
+
execute_command
|
117
|
+
end
|
118
|
+
|
119
|
+
# Normalizes the pending arguments
|
120
|
+
def normalize_pending_arguments(arguments)
|
121
|
+
bad_argument_list!(arguments) unless arguments.empty?
|
122
|
+
end
|
123
|
+
|
124
|
+
# Checks the command
|
125
|
+
def check_command
|
126
|
+
end
|
127
|
+
|
128
|
+
# Executes the command
|
129
|
+
def execute_command
|
130
|
+
end
|
131
|
+
|
132
|
+
end # class Command
|
133
|
+
end # module DbAgile
|
134
|
+
|
135
|
+
# :dba category
|
136
|
+
require 'dbagile/command/dba'
|
137
|
+
require 'dbagile/command/help'
|
138
|
+
|
139
|
+
# :repo category
|
140
|
+
require 'dbagile/command/repo'
|
141
|
+
|
142
|
+
# :db category
|
143
|
+
require 'dbagile/command/db'
|
144
|
+
|
145
|
+
# :bulk category
|
146
|
+
require 'dbagile/command/bulk'
|
147
|
+
|
148
|
+
# :sql category
|
149
|
+
require 'dbagile/command/sql'
|
150
|
+
|
151
|
+
# :schema category
|
152
|
+
require 'dbagile/command/schema'
|
153
|
+
|
154
|
+
# :web category
|
155
|
+
require 'dbagile/command/web'
|
156
|
+
|
157
|
+
# Build Command API now
|
158
|
+
require 'dbagile/command/api'
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module DbAgile
|
2
|
+
module Contract
|
3
|
+
# Connection driven methods of the contract
|
4
|
+
module Connection
|
5
|
+
|
6
|
+
#
|
7
|
+
# Ping the SQL server, returns true if everything is fine. Raises an
|
8
|
+
# error otherwise
|
9
|
+
#
|
10
|
+
# @return true
|
11
|
+
#
|
12
|
+
def ping
|
13
|
+
Kernel.raise NotImplementedError
|
14
|
+
end
|
15
|
+
|
16
|
+
#
|
17
|
+
# Ping the SQL server, returns true if everything is fine, false
|
18
|
+
# otherwise.
|
19
|
+
#
|
20
|
+
def ping?
|
21
|
+
ping
|
22
|
+
true
|
23
|
+
rescue StandardError
|
24
|
+
false
|
25
|
+
end
|
26
|
+
|
27
|
+
#
|
28
|
+
# Returns the database schema as a DbAgile::Core::Schema instance
|
29
|
+
#
|
30
|
+
def physical_schema
|
31
|
+
Kernel.raise NotImplementedError
|
32
|
+
end
|
33
|
+
|
34
|
+
#
|
35
|
+
# Converts a schema abstract script to a SQL script
|
36
|
+
#
|
37
|
+
# @param [DbAgile::Core::Migrate::AbstractScript] a script to run
|
38
|
+
#
|
39
|
+
def script2sql(script, buffer = "")
|
40
|
+
Kernel.raise NotImplementedError
|
41
|
+
end
|
42
|
+
|
43
|
+
#
|
44
|
+
# Disconnect the adapter and frees all resources.
|
45
|
+
#
|
46
|
+
# @return true
|
47
|
+
#
|
48
|
+
def disconnect
|
49
|
+
Kernel.raise NotImplementedError
|
50
|
+
end
|
51
|
+
|
52
|
+
#
|
53
|
+
# Yields the block inside a transaction.
|
54
|
+
#
|
55
|
+
# Adapters are expected to catch the DbAgile::Errors::AbordTransactionError
|
56
|
+
# and to rollback the transaction without re-raising the error.
|
57
|
+
#
|
58
|
+
# @return [...] block's result
|
59
|
+
#
|
60
|
+
def transaction(&block)
|
61
|
+
Kernel.raise NotImplementedError
|
62
|
+
end
|
63
|
+
|
64
|
+
end # module Connection
|
65
|
+
end # module Contract
|
66
|
+
end # module DbAgile
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module DbAgile
|
2
|
+
module Contract
|
3
|
+
module Data
|
4
|
+
module Dataset
|
5
|
+
|
6
|
+
# Makes an allbut selection
|
7
|
+
def allbut(*cs)
|
8
|
+
arr = self.columns - cs.flatten
|
9
|
+
select(*arr)
|
10
|
+
end
|
11
|
+
|
12
|
+
#
|
13
|
+
# Outputs this dataset as a CSV string.
|
14
|
+
#
|
15
|
+
# @return [...] the buffer itself
|
16
|
+
#
|
17
|
+
def to_csv(buffer = "", options = {})
|
18
|
+
DbAgile::IO::CSV::to_csv(self, self.columns, buffer, options)
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
# Outputs this dataset as a JSON string
|
23
|
+
#
|
24
|
+
# @return [...] the buffer itself
|
25
|
+
#
|
26
|
+
def to_json(buffer = "", options = {})
|
27
|
+
DbAgile::IO::JSON::to_json(self.to_a, self.columns, buffer, options)
|
28
|
+
end
|
29
|
+
|
30
|
+
#
|
31
|
+
# Outputs this dataset as a YAML string
|
32
|
+
#
|
33
|
+
# @return [...] the buffer itself
|
34
|
+
#
|
35
|
+
def to_yaml(buffer = "", options = {})
|
36
|
+
DbAgile::IO::YAML::to_yaml(self.to_a, self.columns, buffer, options)
|
37
|
+
end
|
38
|
+
|
39
|
+
#
|
40
|
+
# Outputs this dataset as a XML string
|
41
|
+
#
|
42
|
+
# @return [...] the buffer itself
|
43
|
+
#
|
44
|
+
def to_xml(buffer = "", options = {})
|
45
|
+
DbAgile::IO::XML::to_xml(self, self.columns, buffer, options)
|
46
|
+
end
|
47
|
+
|
48
|
+
#
|
49
|
+
# Outputs this dataset as a Ruby Array of hashes string
|
50
|
+
#
|
51
|
+
# @return [...] the buffer itself
|
52
|
+
#
|
53
|
+
def to_ruby(buffer = "", options = {})
|
54
|
+
DbAgile::IO::Ruby::to_ruby(self, self.columns, buffer, options)
|
55
|
+
end
|
56
|
+
|
57
|
+
#
|
58
|
+
# Outputs this dataset as plain text
|
59
|
+
#
|
60
|
+
# @return [...] the buffer itself
|
61
|
+
#
|
62
|
+
def to_text(buffer = "", options = {})
|
63
|
+
DbAgile::IO::Text::to_text(self, self.columns, buffer, options)
|
64
|
+
end
|
65
|
+
|
66
|
+
end # module Dataset
|
67
|
+
end # module Data
|
68
|
+
end # module Contract
|
69
|
+
end # module DbAgile
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module DbAgile
|
2
|
+
module Contract
|
3
|
+
module Data
|
4
|
+
#
|
5
|
+
# Table driven methods of the contract
|
6
|
+
#
|
7
|
+
module TableDriven
|
8
|
+
|
9
|
+
#
|
10
|
+
# Returns a dataset object for a given table (if a Symbol is given) or query
|
11
|
+
# (if a String is given).
|
12
|
+
#
|
13
|
+
# As DbAgile aims at helping to manage SQL database access with respect to their
|
14
|
+
# schema, it does not specifies a detailed contract about the object returned here,
|
15
|
+
# which is related to queries, not schema modification. The kind of returned object
|
16
|
+
# is therefore left open to adapter specfic implementations.
|
17
|
+
#
|
18
|
+
# We expect (mainly for tests) the following about datasets:
|
19
|
+
# - count: returns the number of records inside the dataset
|
20
|
+
# - to_a: returns an array of hashes representing records
|
21
|
+
#
|
22
|
+
# @param [Symbol | String] table_or_query name of a table or query string
|
23
|
+
# @param [Hash | nil] a tuple projection for query restriction
|
24
|
+
# @return [...] a dataset object with query (execution result)
|
25
|
+
#
|
26
|
+
# @pre [table_or_query] all referenced tables must exist
|
27
|
+
#
|
28
|
+
def dataset(table_or_query, proj = nil)
|
29
|
+
Kernel.raise NotImplementedError
|
30
|
+
end
|
31
|
+
|
32
|
+
#
|
33
|
+
# Checks if a (sub)-tuple exists inside a table.
|
34
|
+
#
|
35
|
+
# @param [Symbol | String] table_or_query name of a table or query string
|
36
|
+
# @param [Hash] subtuple a tuple or tuple projection for the result
|
37
|
+
# @return true if the projection of the query result on subtuple's heading contains
|
38
|
+
# the subtuple itself, false otherwise.
|
39
|
+
#
|
40
|
+
# @pre [table_or_query] all referenced tables must exist
|
41
|
+
#
|
42
|
+
def exists?(table_or_query, subtuple = {})
|
43
|
+
Kernel.raise NotImplementedError
|
44
|
+
end
|
45
|
+
|
46
|
+
end # module TableDriven
|
47
|
+
end # module Data
|
48
|
+
end # module Contract
|
49
|
+
end # module DbAgile
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module DbAgile
|
2
|
+
module Contract
|
3
|
+
module Data
|
4
|
+
#
|
5
|
+
# Transaction driven methods of the contract
|
6
|
+
#
|
7
|
+
module TransactionDriven
|
8
|
+
|
9
|
+
#
|
10
|
+
# Inserts a tuple inside a given table
|
11
|
+
#
|
12
|
+
# @param [Transaction] transaction the current transaction
|
13
|
+
# @param [Symbol] table_name the name of a table
|
14
|
+
# @param [Hash] record a record as a hash (column_name -> value)
|
15
|
+
# @return [Hash] inserted record as a hash
|
16
|
+
#
|
17
|
+
# @pre [table_name] the table must exist
|
18
|
+
# @pre [record] the record is valid for the table
|
19
|
+
# @post the record has been inserted.
|
20
|
+
#
|
21
|
+
def insert(transaction, table_name, record)
|
22
|
+
Kernel.raise NotImplementedError
|
23
|
+
end
|
24
|
+
|
25
|
+
#
|
26
|
+
# Updates all tuples whose projection equal _proj_ with values given by _update_
|
27
|
+
# inside a given table
|
28
|
+
#
|
29
|
+
# @param [Transaction] transaction the current transaction
|
30
|
+
# @param [Symbol] table_name the name of a table
|
31
|
+
# @param [Hash] proj a projection tuple
|
32
|
+
# @return [Hash] update the new values for tuples
|
33
|
+
#
|
34
|
+
# @pre [table_name] the table must exist
|
35
|
+
# @pre [update] the record is valid for the table
|
36
|
+
# @pre [proj] the projection tuple is valid for the table
|
37
|
+
# @post all records have been updated.
|
38
|
+
#
|
39
|
+
def update(transaction, table_name, update, proj = {})
|
40
|
+
Kernel.raise NotImplementedError
|
41
|
+
end
|
42
|
+
|
43
|
+
#
|
44
|
+
# Delete all tuples whose projection equal _proj_ inside a given table
|
45
|
+
#
|
46
|
+
# @param [Transaction] transaction the current transaction
|
47
|
+
# @param [Symbol] table_name the name of a table
|
48
|
+
# @param [Hash] proj a projection tuple
|
49
|
+
#
|
50
|
+
# @pre [table_name] the table must exist
|
51
|
+
# @pre [proj] the projection tuple is valid for the table
|
52
|
+
# @post all records have been updated.
|
53
|
+
#
|
54
|
+
def delete(transaction, table_name, proj = {})
|
55
|
+
Kernel.raise NotImplementedError
|
56
|
+
end
|
57
|
+
|
58
|
+
#
|
59
|
+
# Send SQL directly to the database SQL server.
|
60
|
+
#
|
61
|
+
# Returned result is left opened to adapters.
|
62
|
+
#
|
63
|
+
# @param [Transaction] transaction the current transaction
|
64
|
+
# @param [String] sql a SQL query
|
65
|
+
# @return [...] adapter defined
|
66
|
+
#
|
67
|
+
def direct_sql(transaction, sql)
|
68
|
+
Kernel.raise NotImplementedError
|
69
|
+
end
|
70
|
+
|
71
|
+
end # module TransactionDriven
|
72
|
+
end # module Data
|
73
|
+
end # module Contract
|
74
|
+
end # module DbAgile
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module DbAgile
|
2
|
+
module Contract
|
3
|
+
module Robust
|
4
|
+
module Helpers
|
5
|
+
|
6
|
+
# Asserts that a table exists or raises a NoSuchTableError
|
7
|
+
def has_table!(name, ex = nil)
|
8
|
+
raise NoSuchTableError, "No such table #{name}" unless has_table?(name)
|
9
|
+
end
|
10
|
+
|
11
|
+
# Asserts that a table does not exist or raises a TableAlreadyExistsError
|
12
|
+
def not_has_table!(name, ex = nil)
|
13
|
+
raise TableAlreadyExistsError, "No such table #{name}" if has_table?(name)
|
14
|
+
end
|
15
|
+
|
16
|
+
end # module Helpers
|
17
|
+
end # module Robust
|
18
|
+
end # module Contract
|
19
|
+
end # module DbAgile
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module DbAgile
|
2
|
+
module Contract
|
3
|
+
module Robust
|
4
|
+
class Optimistic
|
5
|
+
module Data
|
6
|
+
module TableDriven
|
7
|
+
|
8
|
+
# @see DbAgile::Contract::Data::TableDriven#dataset
|
9
|
+
def dataset(*args, &block)
|
10
|
+
ds = delegate.dataset(*args, &block)
|
11
|
+
ds.empty?
|
12
|
+
ds
|
13
|
+
rescue StandardError => ex
|
14
|
+
has_table!(args[0], ex)
|
15
|
+
raise
|
16
|
+
end
|
17
|
+
|
18
|
+
# @see DbAgile::Contract::Data::TableDriven#exists?
|
19
|
+
def exists?(*args, &block)
|
20
|
+
delegate.exists?(*args, &block)
|
21
|
+
rescue StandardError => ex
|
22
|
+
has_table!(args[0], ex)
|
23
|
+
raise
|
24
|
+
end
|
25
|
+
|
26
|
+
end # module TableDriven
|
27
|
+
end # module Data
|
28
|
+
end # class Optimistic
|
29
|
+
end # module Robust
|
30
|
+
end # module Contract
|
31
|
+
end # module DbAgile
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module DbAgile
|
2
|
+
module Contract
|
3
|
+
module Robust
|
4
|
+
class Optimistic
|
5
|
+
module Data
|
6
|
+
module TransactionDriven
|
7
|
+
|
8
|
+
# @see ::DbAgile::Contract::Data::TransactionDriven#insert
|
9
|
+
def insert(*args, &block)
|
10
|
+
delegate.insert(*args, &block)
|
11
|
+
rescue
|
12
|
+
has_table!(args[1])
|
13
|
+
raise
|
14
|
+
end
|
15
|
+
|
16
|
+
# @see ::DbAgile::Contract::Data::TransactionDriven#update
|
17
|
+
def update(*args, &block)
|
18
|
+
delegate.update(*args, &block)
|
19
|
+
rescue
|
20
|
+
has_table!(args[1])
|
21
|
+
raise
|
22
|
+
end
|
23
|
+
|
24
|
+
# @see ::DbAgile::Contract::Data::TransactionDriven#delete
|
25
|
+
def delete(*args, &block)
|
26
|
+
delegate.delete(*args, &block)
|
27
|
+
rescue
|
28
|
+
has_table!(args[1])
|
29
|
+
raise
|
30
|
+
end
|
31
|
+
|
32
|
+
# @see ::DbAgile::Contract::Data::TransactionDriven#direct_sql
|
33
|
+
def direct_sql(*args, &block)
|
34
|
+
delegate.direct_sql(*args, &block)
|
35
|
+
rescue
|
36
|
+
has_table!(args[1]) if args[1].kind_of?(::Symbol)
|
37
|
+
raise
|
38
|
+
end
|
39
|
+
|
40
|
+
end # module TransactionDriven
|
41
|
+
end # module Data
|
42
|
+
end # class Optimistic
|
43
|
+
end # module Robust
|
44
|
+
end # module Contract
|
45
|
+
end # module DbAgile
|