Module: TestdataGeneraterForMysql
- Defined in:
- lib/testdata_generater_for_mysql/core.rb,
lib/testdata_generater_for_mysql.rb,
lib/testdata_generater_for_mysql/version.rb
Overview
TODO 以下の削除 サンプルの作成
使い方
・まずは本ファイルをrequiresします
・setup_mysql_settingsでmysql2のclientを内部的に作成(@__clientで作成します)・あとはcreate_rowsメソッドを使ってください・基本的にインサート文のすべてのvalueにシングルクォーテーションで囲まれます。が、列名が「***_at」の場合は除く(もし特定の日時を入れたい場合は Proc.new“‘2001-10-14’”のようにシングルクォーテーションを含めて設定してください・insert__per_rowsにてマルチプルインサートの単位が設定できます(デフォルトは100)
#
# テーブル名
# ループ達(配列で)
# 各列の値の作成方法
#
create_rows(
'm_pkey_or_index__index',
[
[:brand_id,(1..13)],
[:user_id,(1..100)]
],
:brand_id=>Proc.new{|v|v[:brand_id]},
:user_id=>Proc.new{|v|v[:user_id]},
:name=>Proc.new{|v|"#{v[:brand_id]}_#{v[:user_id]}_name"},
:value1=>Proc.new{rand(10000)},
:created_at=>Proc.new{'NOW()'},
)
Constant Summary collapse
- INSERT_PER_ROWS =
100
- VERSION =
"0.0.3"
Instance Method Summary collapse
- #create_rows(table_name, loops, col_procs) ⇒ Object
- #do_insert ⇒ Object
- #get_insert_per_rows ⇒ Object
- #hide_progress_bar ⇒ Object
- #insert_per_rows(v) ⇒ Object
- #looping(ar, index, values = {}) ⇒ Object
- #query(q) ⇒ Object
- #set_insert_values(hash) ⇒ Object
- #setup_mysql_client(hash) ⇒ Object
Instance Method Details
#create_rows(table_name, loops, col_procs) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/testdata_generater_for_mysql/core.rb', line 63 def create_rows(table_name,loops,col_procs) if loops.size == 0 raise 'loops size must be bigger than 0' end total_rows = 0 loops.each_with_index do |l,i| if i == 0 total_rows = l[1].count else total_rows *= l[1].count end end @__table_name = table_name raise 'something wrong' if @__insert_values && @__insert_values.size > 0 @__insert_values = [] @__inserted_rows = 0 @__col_procs = col_procs unless @__disable_progress_bar title = "create rows for #{table_name}" width = 60 $stderr.puts title.center(title.size + 6,' ').center(width,'=') @__pbar = ProgressBar.new('', total_rows, $stderr) @__pbar.format_arguments = [:percentage, :bar, :stat] @__pbar.format = "%3d%% %s %s" end looping(loops,0) do_insert # あまりの作成 if @__pbar @__pbar.finish end end |
#do_insert ⇒ Object
122 123 124 125 126 127 |
# File 'lib/testdata_generater_for_mysql/core.rb', line 122 def do_insert return if @__insert_values.size == 0 @__client.insert(@__client.escape(@__table_name),@__insert_values) @__pbar.inc(@__insert_values.size) if @__pbar @__insert_values = [] end |
#get_insert_per_rows ⇒ Object
51 52 53 |
# File 'lib/testdata_generater_for_mysql/core.rb', line 51 def get_insert_per_rows @__insert_per_rows ||= INSERT_PER_ROWS end |
#hide_progress_bar ⇒ Object
55 56 57 |
# File 'lib/testdata_generater_for_mysql/core.rb', line 55 def @__disable_progress_bar = true end |
#insert_per_rows(v) ⇒ Object
47 48 49 |
# File 'lib/testdata_generater_for_mysql/core.rb', line 47 def insert_per_rows(v) @__insert_per_rows = v end |
#looping(ar, index, values = {}) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/testdata_generater_for_mysql/core.rb', line 98 def looping(ar,index,values={}) ar[index][1].each do |i| values[ar[index][0]] = i next_index = index + 1 if ar.size > next_index looping(ar,next_index,values) elsif ar.size == next_index hash = {} @__col_procs.each do |key,proc| hash[key] = proc.call(values) end set_insert_values(hash) end end end |
#query(q) ⇒ Object
59 60 61 |
# File 'lib/testdata_generater_for_mysql/core.rb', line 59 def query(q) @__client.query(q) end |
#set_insert_values(hash) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/testdata_generater_for_mysql/core.rb', line 114 def set_insert_values(hash) @__insert_values ||= [] @__insert_values << hash if @__insert_values.size > get_insert_per_rows do_insert end end |
#setup_mysql_client(hash) ⇒ Object
42 43 44 45 |
# File 'lib/testdata_generater_for_mysql/core.rb', line 42 def setup_mysql_client(hash) @__client = Mysql2wrapper::Client.new(hash,nil) #@__client = Mysql2wrapper::Client.new(hash) end |