arxutils_sqlite3 0.1.37

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/.rubocop.yml +20 -0
  4. data/.rubocop_todo.yml +387 -0
  5. data/CHANGELOG.md +5 -0
  6. data/CODE_OF_CONDUCT.md +84 -0
  7. data/Gemfile +41 -0
  8. data/LICENSE +21 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +43 -0
  11. data/Rakefile +12 -0
  12. data/arxutils_sqlite3.gemspec +52 -0
  13. data/bin/arxutils-cli +134 -0
  14. data/bin/console +15 -0
  15. data/bin/makemigrate +55 -0
  16. data/bin/setup +8 -0
  17. data/bin/setupx.bat +7 -0
  18. data/bin/setupx.rb +4 -0
  19. data/config/.gitignore +3 -0
  20. data/lib/arxutils_sqlite3/arx.rb +41 -0
  21. data/lib/arxutils_sqlite3/dbutil/dbconnect.rb +95 -0
  22. data/lib/arxutils_sqlite3/dbutil/dbmgr.rb +28 -0
  23. data/lib/arxutils_sqlite3/dbutil.rb +22 -0
  24. data/lib/arxutils_sqlite3/hier.rb +218 -0
  25. data/lib/arxutils_sqlite3/migrate.rb +225 -0
  26. data/lib/arxutils_sqlite3/transactstate.rb +76 -0
  27. data/lib/arxutils_sqlite3/version.rb +4 -0
  28. data/lib/arxutils_sqlite3.rb +40 -0
  29. data/lib/template/config/mysql.tmpl +29 -0
  30. data/lib/template/config/sqlite3.tmpl +26 -0
  31. data/lib/template/relation/base.tmpl +14 -0
  32. data/lib/template/relation/current.tmpl +15 -0
  33. data/lib/template/relation/db_scheme/db_scheme.yml +48 -0
  34. data/lib/template/relation/db_scheme/dbsetup.rb +51 -0
  35. data/lib/template/relation/db_scheme/opts.rb +8 -0
  36. data/lib/template/relation/invalid.tmpl +12 -0
  37. data/lib/template/relation/noitem.tmpl +13 -0
  38. data/lib/template/relation/relation.tmpl +3 -0
  39. data/lib/template/relation/relation_count.tmpl +3 -0
  40. data/lib/template/relation/relation_current.tmpl +3 -0
  41. data/lib/template/relation/relation_invalid.tmpl +4 -0
  42. metadata +85 -0
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/arxutils_sqlite3/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "arxutils_sqlite3"
7
+ spec.version = Arxutils_Sqlite3::VERSION
8
+ spec.authors = ["yasuo kominami"]
9
+ spec.email = ["ykominami@gmail.com"]
10
+
11
+ spec.summary = "utility functions for ActiveRecord."
12
+ spec.description = "utility functions for ActiveRecord."
13
+ spec.homepage = "https://ykominami.github.io/arxutils_sqlite3/"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.5.0"
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'https://mygemserver.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
21
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
22
+ spec.metadata["rubygems_mfa_required"] = "true"
23
+
24
+ # Specify which files should be added to the gem when it is released.
25
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
27
+ `git ls-files -z`.split("\x0").reject do |f|
28
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
29
+ end
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ #spec.add_runtime_dependency "activesupport"
36
+ # spec.add_runtime_dependency "erb"
37
+ # spec.add_runtime_dependency "activerecord", "~> 4.2"
38
+ #spec.add_runtime_dependency "activerecord", "~> 6.1"
39
+ #spec.add_runtime_dependency "sqlite3"
40
+ # spec.add_runtime_dependency "mysql2" , "~> 0.4.1"
41
+ #spec.add_runtime_dependency "encx"
42
+
43
+ # spec.add_development_dependency "bundler", "~> 2.2.10"
44
+ #spec.add_development_dependency "bundler"
45
+ #spec.add_development_dependency "rake", ">= 12.3.3"
46
+ #spec.add_development_dependency "rspec"
47
+ # Uncomment to register a new dependency of your gem
48
+ # spec.add_dependency "example-gem", "~> 1.0"
49
+
50
+ # For more information and examples about making a new gem, checkout our
51
+ # guide at: https://bundler.io/guides/creating_gem.html
52
+ end
data/bin/arxutils-cli ADDED
@@ -0,0 +1,134 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "arxutils_sqlite3"
4
+ require "simpleoptparse"
5
+ require "pp"
6
+ require "ykutils"
7
+ require "ykxutils"
8
+
9
+ if File.exist?(Arxutils_Sqlite3::DEST_OPTS_FILE)
10
+ opts_file = File.join("./" , Arxutils_Sqlite3::DEST_OPTS_FILE_2.to_s)
11
+ begin
12
+ require opts_file
13
+ rescue LoadError => ex
14
+ pp ex.message
15
+ end
16
+ end
17
+
18
+ begin
19
+ require "dbrelation"
20
+ rescue LoadError => ex
21
+ pp ex.message
22
+ end
23
+
24
+ # DBセットアップクラス
25
+ if File.exist?(Arxutils_Sqlite3::DEST_DBSETUP_FILE)
26
+ dbsetup_file = File.join("./" , Arxutils_Sqlite3::DEST_DBSETUP_FILE_2.to_s)
27
+ begin
28
+ require dbsetup_file
29
+ rescue LoadError => ex
30
+ pp ex.message
31
+ end
32
+ end
33
+
34
+
35
+ def prepare_for_migrate(env, db_scheme_ary, opts)
36
+ log_fname = Arxutils_Sqlite3::Dbutil::Dbconnect.make_log_file_name(
37
+ opts["dbconfig"], Arxutils_Sqlite3::Dbutil::DATABASELOG)
38
+ dbconnect = Arxutils_Sqlite3::Dbutil::Dbconnect.new(
39
+ Arxutils_Sqlite3::Dbutil::DB_DIR,
40
+ Arxutils_Sqlite3::Dbutil::MIGRATE_DIR,
41
+ Arxutils_Sqlite3::Dbutil::CONFIG_DIR,
42
+ opts["dbconfig"],
43
+ env,
44
+ log_fname,
45
+ opts
46
+ )
47
+ if opts["cmd"] != "i"
48
+ Arxutils_Sqlite3::Migrate.migrate(dbconnect, db_scheme_ary, opts["migrate_cmd"], opts[:relation], opts)
49
+ end
50
+ dbconnect
51
+ end
52
+
53
+ banner = "Usage: bundle exec ruby exe/makemigrate --cmd=(s|c|m|i|d) -y yaml_file --klass=class"
54
+
55
+ opts = @opts ? @opts : {}
56
+
57
+ opts["dbconfig"] = Arxutils_Sqlite3::Dbutil::DBCONFIG_SQLITE3
58
+
59
+ Simpleoptparse::Simpleoptparse.parse(ARGV, opts, banner, Arxutils_Sqlite3::VERSION, nil) do |parser|
60
+ parser.on("--cmd X", %w[s c m i d]) { |x| opts["cmd"] = x }
61
+ parser.on("-y yaml_file", "--yaml yaml_file") { |x| opts["yaml"] = x }
62
+ parser.on("--klass klass") { |x| opts["klass"] = x }
63
+ end
64
+
65
+ env = ENV.fetch("ENV", nil)
66
+ env ||= "production"
67
+
68
+ case opts["cmd"]
69
+ when "s"
70
+ FileUtils.cp(Arxutils_Sqlite3::DB_SCHEME_FILE, Arxutils_Sqlite3::CONFIG_DIR )
71
+ if opts["klass"].nil? || opts["klass"].strip == ""
72
+ puts banner
73
+ exit 10
74
+ end
75
+
76
+ scope = Object.new
77
+ hash = {klass: opts["klass"]}
78
+ result_content = Ykutils::Erubyx.erubi_render_with_template_file(Arxutils_Sqlite3::OPTS_FILE, scope, hash)
79
+
80
+ File.open(Arxutils_Sqlite3::DEST_OPTS_FILE, "w"){|file|
81
+ file.write(result_content)
82
+ }
83
+ db_scheme_ary = nil
84
+
85
+ when "c"
86
+ opts["migrate_cmd"] = "makeconfig"
87
+ #opts["remigrate"] = false
88
+ db_scheme_ary = nil
89
+
90
+ prepare_for_migrate(env, db_scheme_ary, opts)
91
+ when "m"
92
+ if opts["yaml"].nil? || opts["yaml"].strip == ""
93
+ puts banner
94
+ exit 20
95
+ end
96
+
97
+ opts["migrate_cmd"] = "migrate"
98
+ db_scheme_ary = YAML.load_file( opts["yaml"] )
99
+
100
+ scope = Object.new
101
+ # p "==="
102
+ # p opts
103
+ # p "=== END"
104
+
105
+ hash0 = {module_name: opts[:relation][:module].join("::")}
106
+ hash = db_scheme_ary[0].merge( hash0 )
107
+ result_content = Ykutils::Erubyx.erubi_render_with_template_file(Arxutils_Sqlite3::DBSETUP_FILE, scope, hash)
108
+
109
+ File.open(Arxutils_Sqlite3::DEST_DBSETUP_FILE, "w"){|file|
110
+ file.write(result_content)
111
+ }
112
+ # FileUtils.cp(Arxutils_Sqlite3::DBSETUP_FILE, Arxutils_Sqlite3::CONFIG_DIR )
113
+
114
+ # p "cmd=m"
115
+ dbconnect = prepare_for_migrate(env, db_scheme_ary, opts)
116
+ dbconnect.connect
117
+ #db_migrate_dir = File.join(Dbutil::DB_DIR, Dbutil::MIGRATE_DIR)
118
+ ActiveRecord::MigrationContext.new(dbconnect.migrate_dir, ActiveRecord::SchemaMigration).up
119
+
120
+ when "i"
121
+ #db_scheme_ary = YAML.load_file( opts["yaml"] )
122
+ db_scheme_ary = nil
123
+ dbconnect = prepare_for_migrate(env, db_scheme_ary, opts)
124
+ connect_time = dbconnect.connect
125
+
126
+ Dbsetup.new(connect_time)
127
+
128
+ when "d"
129
+ opts["migrate_cmd"] = "delete"
130
+ db_scheme_ary = nil
131
+ dbconnect = prepare_for_migrate(env, db_scheme_ary, opts)
132
+ else
133
+ raise
134
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "arxutils_sqlite3"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/makemigrate ADDED
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env ruby
2
+ require "arxutils_sqlite3"
3
+ require "simpleoptparse"
4
+
5
+ require "yaml"
6
+
7
+ AR_VERSION = 6.0
8
+
9
+ opts = {
10
+ db_dir: Arxutils_Sqlite3::Dbutil::DB_DIR,
11
+ relation: {
12
+ module: %w[Enop Dbutil],
13
+ filename: "dbrelation.rb",
14
+ dir: "lib/arxutils_sqlite3/dbutil"
15
+ }
16
+ }
17
+ banner = "Usage: bundle exec ruby exe/makemigrate (-c|-r) (--mysql|--sqlite3) -s yaml_file"
18
+
19
+ Simpleoptparse::Simpleoptparse.parse(ARGV, opts, banner, Arxutils_Sqlite3::VERSION, nil) do |parser|
20
+ parser.on("-c", "--makeconfig") { |_x| opts["makeconfig"] = true }
21
+ parser.on("-r", "--remigrate") { |_x| opts["remigrate"] = true }
22
+ parser.on("-s yaml_file", "--setting yaml_file") { |file| opts["setting"] = file }
23
+ parser.on("--mysql") { |_x| opts["dbconfig"] = Arxutils_Sqlite3::Dbutil::DBCONFIG_MYSQL }
24
+ parser.on("--sqlite3") { |_x| opts["dbconfig"] = Arxutils_Sqlite3::Dbutil::DBCONFIG_SQLITE3 }
25
+ end
26
+
27
+ db_scheme_yaml_file = opts["setting"]
28
+
29
+ if db_scheme_yaml_file.nil?
30
+ puts banner
31
+ exit 10
32
+ end
33
+
34
+ opts["makeconfig"] = !opts["makeconfig"].nil?
35
+ opts["remigrate"] = !opts["remigrate"].nil?
36
+
37
+ db_scheme_ary = YAML.load_file(db_scheme_yaml_file)
38
+ #pp db_scheme_ary
39
+
40
+ opts["dbconfig"] = Arxutils_Sqlite3::Dbutil::DBCONFIG_SQLITE3 unless opts["dbconfig"]
41
+
42
+ env = ENV.fetch("ENV", nil)
43
+ # env ||= "development"
44
+ env ||= "production"
45
+
46
+ Arxutils_Sqlite3::Migrate.migrate(
47
+ Arxutils_Sqlite3::Dbutil::DB_DIR,
48
+ Arxutils_Sqlite3::Dbutil::CONFIG_DIR,
49
+ Arxutils_Sqlite3::Dbutil::DATABASELOG,
50
+ Arxutils_Sqlite3::Dbutil::MIGRATE_DIR,
51
+ env,
52
+ db_scheme_ary,
53
+ opts["dbconfig"],
54
+ opts
55
+ )
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/bin/setupx.bat ADDED
@@ -0,0 +1,7 @@
1
+ REM set -euo pipefail
2
+ REM IFS=$'\n\t'
3
+ REM set -vx
4
+
5
+ REM bundle install
6
+ bundle exec ruby bin/setupx.rb
7
+ # Do any other automated setup that you need to do here
data/bin/setupx.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "arxutils_sqlite3"
2
+ require "pathname"
3
+
4
+ puts $PROGRAM_NAME
data/config/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ *
2
+ !.gitignore
3
+
@@ -0,0 +1,41 @@
1
+ require "erb"
2
+ require "ykutils"
3
+ module Arxutils_Sqlite3
4
+ # スキーマ設定に基づき、テンプレートから変換する
5
+ class Arx
6
+ @field = nil
7
+ # スキーマ設定配列を、テンプレートで参照可能になるように展開する
8
+ def initialize(data, fname)
9
+ # テンプレートファイルへのパス
10
+ @fname = fname
11
+
12
+ # スキーマ設定配列
13
+ # スキーマ設定とは以下をキーにもつハッシュである
14
+ # :flist
15
+ # :classname
16
+ # :classname_downcase
17
+ # :items
18
+ # フィールド名, 型, null許容 の配列
19
+ # :plural
20
+ # :relation
21
+ @data = data
22
+
23
+ # スキーマ設定の:itemsの値を展開後格納するためのStructクラス
24
+ #@field ||= Struct.new("Field", :name, :type, :null)
25
+ @field ||= Struct.new(:name, :type, :null)
26
+
27
+ @data[:ary] = if @data[:items]
28
+ @data[:items].map { |x| @field.new(*x) }
29
+ else
30
+ []
31
+ end
32
+ end
33
+
34
+ # テンプレートファイルを元にした変換結果を返す
35
+ def create
36
+ scope = Object.new
37
+ scope.instance_variable_set(:@data , @data)
38
+ Ykutils::Erubyx.erubi_render_with_template_file(@fname, scope)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,95 @@
1
+ #! -*- encoding : UTF-8 -*-
2
+
3
+ require "fileutils"
4
+ require "yaml"
5
+ require "active_record"
6
+ require "sqlite3"
7
+ require "ykxutils"
8
+
9
+ def require_x(path)
10
+ require path
11
+ rescue StandardError => e
12
+ p "1 request_x"
13
+ p e
14
+ rescue LoadError => e
15
+ p "21 request_x"
16
+ p e
17
+ rescue NotImplementedError => e
18
+ p "22 request_x"
19
+ p e
20
+ rescue SyntaxError => e
21
+ p "23 request_x"
22
+ p e
23
+ rescue ScriptError => e
24
+ p "20 request_x"
25
+ p e
26
+ rescue Exception => e
27
+ p "9 request_x"
28
+ p e
29
+ end
30
+
31
+ module Arxutils_Sqlite3
32
+ module Dbutil
33
+ # DB操作用ユーティリティクラス
34
+ class Dbconnect
35
+ # 生成するDB構成情報ファイルパス
36
+ attr_accessor :dbconfig_dest_path
37
+ # 参照用DB構成情報ファイル名
38
+ attr_accessor :dbconfig_src_fname
39
+ # migrate用スクリプトの出力先ディレクトリ名
40
+ attr_accessor :migrate_dir, :dest_config_dir, :db_dir
41
+
42
+ def self.make_log_file_name(dbconfig, log_file_base_name)
43
+ format("%s-%s", dbconfig.to_s, log_file_base_name)
44
+ end
45
+
46
+ # DB接続までの初期化に必要なディレクトリの確認、作成
47
+ def initialize(db_dir, migrate_base_dir, src_config_dir, dbconfig, env, log_fname, opts)
48
+ # 接続開始時刻
49
+ @connect_time = nil
50
+ # DB格納ディレクトリ名
51
+ @db_dir = db_dir
52
+ # DB構成ファイルのテンプレート格納ディレクトリ
53
+ @src_config_dir = src_config_dir
54
+ # DB構成ファイルの出力先ディレクトリ
55
+ @dest_config_dir = CONFIG_DIR
56
+ # DB構成ファイル名
57
+ @dbconfig_dest_fname = "#{dbconfig}.yml"
58
+ # DB構成ファイル用テンプレートファイル名
59
+ @dbconfig_src_fname = "#{dbconfig}.tmpl"
60
+ # DB構成ファイルへのパス
61
+ @dbconfig_dest_path = File.join(@dest_config_dir, @dbconfig_dest_fname)
62
+ # DB構成ファイル用テンプレートファイルへのパス
63
+ @dbconfig_src_path = File.join(@src_config_dir, @dbconfig_src_fname)
64
+ # 環境の指定
65
+ @env = env
66
+ # DB用ログファイル名
67
+ @log_fname = log_fname
68
+
69
+ if @db_dir && @log_fname
70
+ # DB用ログファイルへのパス
71
+ @log_path = File.join(@db_dir, @log_fname)
72
+ # migrate用スクリプト格納ディレクトリへのパス
73
+ @migrate_dir = File.join(@db_dir, migrate_base_dir)
74
+ end
75
+ FileUtils.mkdir_p(@db_dir) if @db_dir
76
+ FileUtils.mkdir_p(@migrate_dir) if @migrate_dir
77
+ FileUtils.mkdir_p(@dest_config_dir)
78
+ end
79
+
80
+ # DB接続、DB用ログファイルの設定
81
+ def connect
82
+ unless @connect_time
83
+ begin
84
+ dbconfig = Ykxutils.yaml_load_file_compati(@dbconfig_dest_path)
85
+ ActiveRecord::Base.establish_connection(dbconfig[@env])
86
+ ActiveRecord::Base.logger = Logger.new(@log_path)
87
+ @connect_time = DateTime.now.new_offset
88
+ rescue => ex
89
+ end
90
+ end
91
+ @connect_time
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,28 @@
1
+ require "arxutils_sqlite3/dbutil/dbinit"
2
+ require "date"
3
+ require "pp"
4
+
5
+ module Arxutils_Sqlite3
6
+ module Dbutil
7
+ # DB接続時に、現在日時も取得したい場合のユーティリティクラス
8
+ class DbMgr
9
+ # DB接続の状態を示す
10
+ @ret = nil
11
+ # DB接続、現在日時取得
12
+ def self.setup(dbinit)
13
+ unless @ret
14
+ begin
15
+ dbinit.setup
16
+ @ret = DateTime.now.new_offset
17
+ rescue StandardError => e
18
+ pp e.class
19
+ pp e.message
20
+ pp e.backtrace
21
+ end
22
+ end
23
+
24
+ @ret
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ require_relative "dbutil/dbconnect"
2
+ #require_relative "dbutil/dbmgr"
3
+
4
+ module Arxutils_Sqlite3
5
+ # DB操作用ユーティリティクラス
6
+ module Dbutil
7
+ # DB格納ディレクトリ名
8
+ DB_DIR = "db".freeze
9
+ # migrate用スクリプト格納ディレクトリ名
10
+ MIGRATE_DIR = "migrate".freeze
11
+ # SQLITE3用DB構成名
12
+ DBCONFIG_SQLITE3 = "sqlite3".freeze
13
+ # MYSQL用DB構成名
14
+ DBCONFIG_MYSQL = "mysql".freeze
15
+ # DB構成格納用ディレクトリ名
16
+ CONFIG_DIR = "config".freeze
17
+ # データベース用ログファイル名
18
+ DATABASELOG = "database.log".freeze
19
+
20
+ # DB接続までの初期化を行う
21
+ end
22
+ end