dbagile 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (305) hide show
  1. data/LICENCE.textile +12 -0
  2. data/README.textile +89 -0
  3. data/bin/dba +22 -0
  4. data/lib/dbagile/adapter/sequel/class_methods.rb +18 -0
  5. data/lib/dbagile/adapter/sequel/connection.rb +38 -0
  6. data/lib/dbagile/adapter/sequel/data/table_driven.rb +30 -0
  7. data/lib/dbagile/adapter/sequel/data/transaction_driven.rb +43 -0
  8. data/lib/dbagile/adapter/sequel/schema/concrete_script.rb +135 -0
  9. data/lib/dbagile/adapter/sequel/schema/physical_dump.rb +106 -0
  10. data/lib/dbagile/adapter/sequel/schema/schema2sequel_args.rb +71 -0
  11. data/lib/dbagile/adapter/sequel/schema/table_driven.rb +52 -0
  12. data/lib/dbagile/adapter/sequel/schema/transaction_driven.rb +46 -0
  13. data/lib/dbagile/adapter/sequel/sequel_tracer.rb +144 -0
  14. data/lib/dbagile/adapter/sequel.rb +46 -0
  15. data/lib/dbagile/adapter.rb +15 -0
  16. data/lib/dbagile/command/api.rb +49 -0
  17. data/lib/dbagile/command/bulk/commons.rb +130 -0
  18. data/lib/dbagile/command/bulk/export.rb +99 -0
  19. data/lib/dbagile/command/bulk/import.rb +147 -0
  20. data/lib/dbagile/command/bulk.rb +3 -0
  21. data/lib/dbagile/command/class_methods.rb +103 -0
  22. data/lib/dbagile/command/db/add.rb +94 -0
  23. data/lib/dbagile/command/db/list.rb +40 -0
  24. data/lib/dbagile/command/db/ping.rb +49 -0
  25. data/lib/dbagile/command/db/rm.rb +52 -0
  26. data/lib/dbagile/command/db/stage.rb +81 -0
  27. data/lib/dbagile/command/db/use.rb +48 -0
  28. data/lib/dbagile/command/db.rb +6 -0
  29. data/lib/dbagile/command/dba.rb +121 -0
  30. data/lib/dbagile/command/help.rb +50 -0
  31. data/lib/dbagile/command/repo/create.rb +54 -0
  32. data/lib/dbagile/command/repo.rb +1 -0
  33. data/lib/dbagile/command/robust.rb +86 -0
  34. data/lib/dbagile/command/schema/check.rb +59 -0
  35. data/lib/dbagile/command/schema/commons.rb +118 -0
  36. data/lib/dbagile/command/schema/diff.rb +101 -0
  37. data/lib/dbagile/command/schema/dump.rb +48 -0
  38. data/lib/dbagile/command/schema/merge.rb +55 -0
  39. data/lib/dbagile/command/schema/sql_script.rb +124 -0
  40. data/lib/dbagile/command/schema.rb +6 -0
  41. data/lib/dbagile/command/sql/drop.rb +40 -0
  42. data/lib/dbagile/command/sql/heading.rb +34 -0
  43. data/lib/dbagile/command/sql/send.rb +67 -0
  44. data/lib/dbagile/command/sql/show.rb +42 -0
  45. data/lib/dbagile/command/sql.rb +4 -0
  46. data/lib/dbagile/command/web/tools.rb +23 -0
  47. data/lib/dbagile/command/web.rb +1 -0
  48. data/lib/dbagile/command.rb +158 -0
  49. data/lib/dbagile/contract/connection.rb +66 -0
  50. data/lib/dbagile/contract/data/dataset.rb +69 -0
  51. data/lib/dbagile/contract/data/table_driven.rb +49 -0
  52. data/lib/dbagile/contract/data/transaction_driven.rb +74 -0
  53. data/lib/dbagile/contract/data.rb +3 -0
  54. data/lib/dbagile/contract/robust/helpers.rb +19 -0
  55. data/lib/dbagile/contract/robust/optimistic/data/table_driven.rb +31 -0
  56. data/lib/dbagile/contract/robust/optimistic/data/transaction_driven.rb +45 -0
  57. data/lib/dbagile/contract/robust/optimistic/schema/table_driven.rb +53 -0
  58. data/lib/dbagile/contract/robust/optimistic/schema/transaction_driven.rb +45 -0
  59. data/lib/dbagile/contract/robust/optimistic.rb +18 -0
  60. data/lib/dbagile/contract/robust.rb +20 -0
  61. data/lib/dbagile/contract/schema/table_driven.rb +89 -0
  62. data/lib/dbagile/contract/schema/transaction_driven.rb +68 -0
  63. data/lib/dbagile/contract/schema.rb +2 -0
  64. data/lib/dbagile/contract/utils/delegate.rb +17 -0
  65. data/lib/dbagile/contract/utils/full.rb +13 -0
  66. data/lib/dbagile/contract/utils.rb +2 -0
  67. data/lib/dbagile/contract.rb +5 -0
  68. data/lib/dbagile/core/chain.rb +92 -0
  69. data/lib/dbagile/core/connection.rb +51 -0
  70. data/lib/dbagile/core/database.rb +221 -0
  71. data/lib/dbagile/core/io/dsl.rb +95 -0
  72. data/lib/dbagile/core/io/robustness.rb +82 -0
  73. data/lib/dbagile/core/io.rb +2 -0
  74. data/lib/dbagile/core/repository/builder.rb +67 -0
  75. data/lib/dbagile/core/repository/yaml_methods.rb +82 -0
  76. data/lib/dbagile/core/repository.rb +211 -0
  77. data/lib/dbagile/core/schema/builder/coercion.rb +210 -0
  78. data/lib/dbagile/core/schema/builder/concept_factory.rb +61 -0
  79. data/lib/dbagile/core/schema/builder.rb +187 -0
  80. data/lib/dbagile/core/schema/composite.rb +239 -0
  81. data/lib/dbagile/core/schema/computations/filter.rb +40 -0
  82. data/lib/dbagile/core/schema/computations/merge.rb +55 -0
  83. data/lib/dbagile/core/schema/computations/minus.rb +44 -0
  84. data/lib/dbagile/core/schema/computations/split.rb +37 -0
  85. data/lib/dbagile/core/schema/computations.rb +4 -0
  86. data/lib/dbagile/core/schema/database_schema.rb +129 -0
  87. data/lib/dbagile/core/schema/errors.rb +173 -0
  88. data/lib/dbagile/core/schema/logical/attribute.rb +68 -0
  89. data/lib/dbagile/core/schema/logical/constraint/candidate_key.rb +70 -0
  90. data/lib/dbagile/core/schema/logical/constraint/foreign_key.rb +121 -0
  91. data/lib/dbagile/core/schema/logical/constraint.rb +58 -0
  92. data/lib/dbagile/core/schema/logical/constraints.rb +28 -0
  93. data/lib/dbagile/core/schema/logical/heading.rb +47 -0
  94. data/lib/dbagile/core/schema/logical/relvar.rb +81 -0
  95. data/lib/dbagile/core/schema/logical.rb +24 -0
  96. data/lib/dbagile/core/schema/migrate/abstract_script.rb +35 -0
  97. data/lib/dbagile/core/schema/migrate/collapse_table.rb +15 -0
  98. data/lib/dbagile/core/schema/migrate/create_table.rb +15 -0
  99. data/lib/dbagile/core/schema/migrate/drop_table.rb +15 -0
  100. data/lib/dbagile/core/schema/migrate/expand_table.rb +15 -0
  101. data/lib/dbagile/core/schema/migrate/operation.rb +141 -0
  102. data/lib/dbagile/core/schema/migrate/stager.rb +282 -0
  103. data/lib/dbagile/core/schema/migrate.rb +2 -0
  104. data/lib/dbagile/core/schema/part.rb +114 -0
  105. data/lib/dbagile/core/schema/physical/index.rb +64 -0
  106. data/lib/dbagile/core/schema/physical/indexes.rb +12 -0
  107. data/lib/dbagile/core/schema/physical.rb +26 -0
  108. data/lib/dbagile/core/schema/robustness.rb +39 -0
  109. data/lib/dbagile/core/schema/schema_object.rb +94 -0
  110. data/lib/dbagile/core/schema.rb +254 -0
  111. data/lib/dbagile/core/transaction.rb +74 -0
  112. data/lib/dbagile/core.rb +7 -0
  113. data/lib/dbagile/environment/buffering.rb +45 -0
  114. data/lib/dbagile/environment/delegator.rb +59 -0
  115. data/lib/dbagile/environment/interactions.rb +208 -0
  116. data/lib/dbagile/environment/on_error.rb +47 -0
  117. data/lib/dbagile/environment/repository.rb +161 -0
  118. data/lib/dbagile/environment/robustness.rb +7 -0
  119. data/lib/dbagile/environment/testing.rb +23 -0
  120. data/lib/dbagile/environment.rb +122 -0
  121. data/lib/dbagile/errors.rb +30 -0
  122. data/lib/dbagile/io/csv.rb +99 -0
  123. data/lib/dbagile/io/json.rb +41 -0
  124. data/lib/dbagile/io/pretty_table.rb +128 -0
  125. data/lib/dbagile/io/ruby.rb +62 -0
  126. data/lib/dbagile/io/text.rb +18 -0
  127. data/lib/dbagile/io/type_safe.rb +65 -0
  128. data/lib/dbagile/io/xml.rb +35 -0
  129. data/lib/dbagile/io/yaml.rb +30 -0
  130. data/lib/dbagile/io.rb +94 -0
  131. data/lib/dbagile/loader.rb +16 -0
  132. data/lib/dbagile/plugin.rb +29 -0
  133. data/lib/dbagile/restful/client/delete.rb +22 -0
  134. data/lib/dbagile/restful/client/get.rb +24 -0
  135. data/lib/dbagile/restful/client/post.rb +22 -0
  136. data/lib/dbagile/restful/client/utils.rb +16 -0
  137. data/lib/dbagile/restful/client.rb +41 -0
  138. data/lib/dbagile/restful/middleware/delete.rb +22 -0
  139. data/lib/dbagile/restful/middleware/get.rb +27 -0
  140. data/lib/dbagile/restful/middleware/one_database.rb +82 -0
  141. data/lib/dbagile/restful/middleware/post.rb +23 -0
  142. data/lib/dbagile/restful/middleware/utils.rb +65 -0
  143. data/lib/dbagile/restful/middleware.rb +54 -0
  144. data/lib/dbagile/restful/server.rb +65 -0
  145. data/lib/dbagile/restful.rb +9 -0
  146. data/lib/dbagile/robustness/dependencies.rb +36 -0
  147. data/lib/dbagile/robustness/file_system.rb +53 -0
  148. data/lib/dbagile/robustness.rb +14 -0
  149. data/lib/dbagile/tools/file_system.rb +24 -0
  150. data/lib/dbagile/tools/math.rb +11 -0
  151. data/lib/dbagile/tools/ordered_hash.rb +39 -0
  152. data/lib/dbagile/tools/ruby.rb +78 -0
  153. data/lib/dbagile/tools/string.rb +6 -0
  154. data/lib/dbagile/tools/tuple.rb +49 -0
  155. data/lib/dbagile/tools.rb +6 -0
  156. data/lib/dbagile.rb +66 -0
  157. data/test/assumptions/equality.spec +11 -0
  158. data/test/assumptions/fixtures.rb +39 -0
  159. data/test/assumptions/inheritance.spec +17 -0
  160. data/test/assumptions/sequel/autonumber.spec +19 -0
  161. data/test/assumptions/sequel/connect.spec +29 -0
  162. data/test/assumptions/sequel/test.db +0 -0
  163. data/test/assumptions/stdlib/pathname.spec +13 -0
  164. data/test/assumptions/yaml/fixtures.rb +25 -0
  165. data/test/assumptions/yaml/to_yaml.spec +10 -0
  166. data/test/assumptions.spec +2 -0
  167. data/test/commands/bulk/export.spec +100 -0
  168. data/test/commands/bulk/import.spec +49 -0
  169. data/test/commands/db/add.spec +31 -0
  170. data/test/commands/db/list.spec +29 -0
  171. data/test/commands/db/ping.spec +21 -0
  172. data/test/commands/db/rm.spec +18 -0
  173. data/test/commands/db/use.spec +18 -0
  174. data/test/commands/dba.spec +54 -0
  175. data/test/commands/repo/create.spec +30 -0
  176. data/test/commands/schema/check.spec +25 -0
  177. data/test/commands/schema/diff.spec +30 -0
  178. data/test/commands/schema/dump.spec +23 -0
  179. data/test/commands/schema/fixtures/add_constraint.yaml +30 -0
  180. data/test/commands/schema/fixtures/announced.yaml +28 -0
  181. data/test/commands/schema/fixtures/effective.yaml +20 -0
  182. data/test/commands/schema/fixtures/invalid.yaml +6 -0
  183. data/test/commands/schema/sql_script.spec +56 -0
  184. data/test/commands/sql/drop.spec +25 -0
  185. data/test/commands/sql/heading.spec +7 -0
  186. data/test/commands/sql/scripts/delete.sql +1 -0
  187. data/test/commands/sql/scripts/insert.sql +2 -0
  188. data/test/commands/sql/send.spec +29 -0
  189. data/test/commands/sql/show.spec +17 -0
  190. data/test/commands.spec +138 -0
  191. data/test/contract/connection/transaction.ex +11 -0
  192. data/test/contract/connection.spec +9 -0
  193. data/test/contract/data/dataset/columns.ex +5 -0
  194. data/test/contract/data/dataset/count.ex +5 -0
  195. data/test/contract/data/dataset.spec +9 -0
  196. data/test/contract/data/table_driven/dataset.ex +31 -0
  197. data/test/contract/data/table_driven/exists_q.ex +27 -0
  198. data/test/contract/data/table_driven.spec +9 -0
  199. data/test/contract/data/transaction_driven/delete.ex +29 -0
  200. data/test/contract/data/transaction_driven/direct_sql.ex +19 -0
  201. data/test/contract/data/transaction_driven/insert.ex +8 -0
  202. data/test/contract/data/transaction_driven/update.ex +19 -0
  203. data/test/contract/data/transaction_driven.spec +18 -0
  204. data/test/contract/robust/data/table_driven.spec +15 -0
  205. data/test/contract/robust/data/transaction_driven.spec +21 -0
  206. data/test/contract/robust/schema/table_driven.spec +21 -0
  207. data/test/contract/robust/schema/transaction_driven.spec +19 -0
  208. data/test/contract/schema/table_driven/column_names.ex +5 -0
  209. data/test/contract/schema/table_driven/has_column_q.ex +13 -0
  210. data/test/contract/schema/table_driven/has_table_q.ex +11 -0
  211. data/test/contract/schema/table_driven/heading.ex +5 -0
  212. data/test/contract/schema/table_driven.spec +9 -0
  213. data/test/contract/schema/transaction_driven/create_table.ex +10 -0
  214. data/test/contract/schema/transaction_driven/drop_table.ex +10 -0
  215. data/test/contract/schema/transaction_driven.spec +18 -0
  216. data/test/contract.spec +66 -0
  217. data/test/fixtures/basics/data/basic_values.rb +13 -0
  218. data/test/fixtures/basics/data/empty_table.rb +3 -0
  219. data/test/fixtures/basics/data/non_empty_table.rb +4 -0
  220. data/test/fixtures/basics/data/parts.rb +8 -0
  221. data/test/fixtures/basics/data/suppliers.rb +7 -0
  222. data/test/fixtures/basics/data/supplies.rb +14 -0
  223. data/test/fixtures/basics/dbagile.idx +20 -0
  224. data/test/fixtures/basics/fixtures.yaml +28 -0
  225. data/test/fixtures/basics/robust.db +0 -0
  226. data/test/fixtures/basics/suppliers.yaml +30 -0
  227. data/test/fixtures/basics/test.db +0 -0
  228. data/test/fixtures/empty/dbagile.idx +5 -0
  229. data/test/fixtures.rb +152 -0
  230. data/test/restful/delete/no_format.ex +32 -0
  231. data/test/restful/delete.spec +8 -0
  232. data/test/restful/get/csv_format.ex +12 -0
  233. data/test/restful/get/json_format.ex +19 -0
  234. data/test/restful/get/query_string.ex +11 -0
  235. data/test/restful/get/text_format.ex +12 -0
  236. data/test/restful/get/yaml_format.ex +14 -0
  237. data/test/restful/get.spec +5 -0
  238. data/test/restful/post/no_format.ex +22 -0
  239. data/test/restful/post.spec +8 -0
  240. data/test/restful.spec +32 -0
  241. data/test/run_all_suite.rb +51 -0
  242. data/test/spec_helper.rb +26 -0
  243. data/test/support/be_a_valid_json_string.rb +19 -0
  244. data/test/support/be_a_valid_yaml_string.rb +18 -0
  245. data/test/unit/adapter/factor.spec +13 -0
  246. data/test/unit/adapter/sequel/new.spec +19 -0
  247. data/test/unit/command/api.spec +12 -0
  248. data/test/unit/command/command_for.spec +36 -0
  249. data/test/unit/command/command_name_of.spec +21 -0
  250. data/test/unit/command/ruby_method_for.spec +21 -0
  251. data/test/unit/command/sanity.spec +34 -0
  252. data/test/unit/contract/utils/delegate/delegate.spec +23 -0
  253. data/test/unit/core/chain/chain.spec +57 -0
  254. data/test/unit/core/chain/connect.spec +22 -0
  255. data/test/unit/core/chain/delegate_chain.spec +16 -0
  256. data/test/unit/core/chain/initialize.spec +19 -0
  257. data/test/unit/core/chain/plug.spec +31 -0
  258. data/test/unit/core/io/dsl/scope.spec +9 -0
  259. data/test/unit/core/repository/create_bang.spec +31 -0
  260. data/test/unit/core/repository/current.spec +31 -0
  261. data/test/unit/core/repository/database.spec +47 -0
  262. data/test/unit/core/repository/fixtures/corrupted/dbagile.idx +1 -0
  263. data/test/unit/core/repository/fixtures/test_and_prod/dbagile.idx +21 -0
  264. data/test/unit/core/repository/fixtures.rb +25 -0
  265. data/test/unit/core/repository/has_database_q.spec +16 -0
  266. data/test/unit/core/repository/load.spec +51 -0
  267. data/test/unit/core/repository/to_yaml.spec +17 -0
  268. data/test/unit/core/schema/check.spec +32 -0
  269. data/test/unit/core/schema/empty_q.spec +18 -0
  270. data/test/unit/core/schema/filter.spec +42 -0
  271. data/test/unit/core/schema/fixtures/dbagile.yaml +7 -0
  272. data/test/unit/core/schema/fixtures/empty.yaml +11 -0
  273. data/test/unit/core/schema/fixtures/invalid.yaml +54 -0
  274. data/test/unit/core/schema/fixtures/left.yaml +46 -0
  275. data/test/unit/core/schema/fixtures/left_minus_right.yaml +31 -0
  276. data/test/unit/core/schema/fixtures/right.yaml +46 -0
  277. data/test/unit/core/schema/fixtures/right_minus_left.yaml +31 -0
  278. data/test/unit/core/schema/fixtures/suppliers_and_parts.yaml +30 -0
  279. data/test/unit/core/schema/fixtures.rb +32 -0
  280. data/test/unit/core/schema/merge.spec +72 -0
  281. data/test/unit/core/schema/minus.spec +26 -0
  282. data/test/unit/core/schema/sanity.spec +39 -0
  283. data/test/unit/core/schema/split.spec +58 -0
  284. data/test/unit/core/schema/stage_script.spec +26 -0
  285. data/test/unit/core/schema/to_yaml.spec +13 -0
  286. data/test/unit/core/schema/yaml_display.spec +14 -0
  287. data/test/unit/core/schema/yaml_load.spec +20 -0
  288. data/test/unit/core/transaction/transaction.spec +10 -0
  289. data/test/unit/fixtures.rb +67 -0
  290. data/test/unit/io/to_xxx.spec +52 -0
  291. data/test/unit/plugin/options.spec +21 -0
  292. data/test/unit/plugin/tuple_heading.spec +11 -0
  293. data/test/unit/plugin/with_options.spec +12 -0
  294. data/test/unit/tools/ruby/class_unqualified_name.spec +26 -0
  295. data/test/unit/tools/ruby/extract_file_rdoc.spec +10 -0
  296. data/test/unit/tools/ruby/fixtures/rdoc.txt +12 -0
  297. data/test/unit/tools/ruby/fixtures.rb +19 -0
  298. data/test/unit/tools/ruby/optional_args_block_call.spec +35 -0
  299. data/test/unit/tools/ruby/parent_module.spec +21 -0
  300. data/test/unit/tools/ruby/rdoc_file_paragraphs.spec +13 -0
  301. data/test/unit/tools/tuple/tuple_heading.spec +11 -0
  302. data/test/unit/tools/tuple/tuple_key.spec +27 -0
  303. data/test/unit/tools/tuple/tuple_project.spec +23 -0
  304. data/test/unit.spec +3 -0
  305. 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,4 @@
1
+ require 'dbagile/command/sql/show'
2
+ require 'dbagile/command/sql/send'
3
+ require 'dbagile/command/sql/heading'
4
+ require 'dbagile/command/sql/drop'
@@ -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,3 @@
1
+ require 'dbagile/contract/data/dataset'
2
+ require 'dbagile/contract/data/table_driven'
3
+ require 'dbagile/contract/data/transaction_driven'
@@ -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