mydbi 1.0.5
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.
- data/History.txt +12 -0
- data/Manifest.txt +5 -0
- data/README.txt +114 -0
- data/Rakefile +13 -0
- data/lib/mydbi.rb +170 -0
- metadata +104 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
= mydbi
|
2
|
+
|
3
|
+
http://github.com/andynu/mydbi
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
A simple wrapper for dbi that provides two simple functions dbconnect(profile) and query(sql),
|
8
|
+
along with auth profiles via a configuration file.
|
9
|
+
|
10
|
+
== FEATURES/PROBLEMS:
|
11
|
+
|
12
|
+
* PROBLEM: no tests => I have some, but they point at local databases and I have not gotten around to making nice mocky ones.
|
13
|
+
|
14
|
+
== SYNOPSIS:
|
15
|
+
|
16
|
+
DISCLAMER: The DBI api is fine and in most contexts should be used directly.
|
17
|
+
I wrote it as a little helper for the hundreds of tiny csv outputers,
|
18
|
+
backfills, and other assorted one-off sys-admin data munghing scripts scripts
|
19
|
+
that I write. This script intoduces three methods into the Object namespace, it
|
20
|
+
should not be used in a larger project. Please keep that in mind.
|
21
|
+
|
22
|
+
A simple wrapper for dbi that provides two simple functions:
|
23
|
+
|
24
|
+
dbconnect(dbname="test", host="localhost", user="root", pass="")
|
25
|
+
dbconnect(profile_key)
|
26
|
+
|
27
|
+
uses my most common defaults
|
28
|
+
|
29
|
+
query(sql, *values)
|
30
|
+
|
31
|
+
which returns the last_insert_id() for auto increment table inserts
|
32
|
+
and yields row when selecting.
|
33
|
+
|
34
|
+
ascii_query(sql, *values)
|
35
|
+
|
36
|
+
select query output much like mysql's commandline client
|
37
|
+
|
38
|
+
== Config file $HOME/.mydbirc
|
39
|
+
|
40
|
+
A yaml configuration file for named database connections
|
41
|
+
|
42
|
+
---
|
43
|
+
:databaseone:
|
44
|
+
:name: db_one
|
45
|
+
:host: localhost
|
46
|
+
:username: root
|
47
|
+
:password:
|
48
|
+
:databasetwo:
|
49
|
+
:name: db_two
|
50
|
+
:host: other_host
|
51
|
+
:username: root
|
52
|
+
:password: secret
|
53
|
+
|
54
|
+
Then you can pass the symbol is as the db name
|
55
|
+
|
56
|
+
db1 = dbconnect(:databaseone)
|
57
|
+
|
58
|
+
db2 = dbconnect(:databasetwo)
|
59
|
+
|
60
|
+
== Example:
|
61
|
+
|
62
|
+
require 'mydbi'
|
63
|
+
|
64
|
+
dbconnect(:databaseone)
|
65
|
+
|
66
|
+
lastn_id = query("insert into lastn (id, ord, song_id) values (null, 1, 10)");
|
67
|
+
|
68
|
+
query("select * from lastn") do |row|
|
69
|
+
puts row.inspect
|
70
|
+
end
|
71
|
+
|
72
|
+
== Resources
|
73
|
+
|
74
|
+
* http://ruby-dbi.rubyforge.org/
|
75
|
+
* http://www.kitebird.com/articles/ruby-dbi.html
|
76
|
+
|
77
|
+
== REQUIREMENTS:
|
78
|
+
|
79
|
+
* dbi
|
80
|
+
* dbd-mysql
|
81
|
+
* yamlrc
|
82
|
+
|
83
|
+
== INSTALL:
|
84
|
+
|
85
|
+
(if you haven't already)
|
86
|
+
> sudo gem install gem_cutter
|
87
|
+
> gem tumble
|
88
|
+
|
89
|
+
> sudo gem install mydbi
|
90
|
+
|
91
|
+
== LICENSE:
|
92
|
+
|
93
|
+
(The MIT License)
|
94
|
+
|
95
|
+
Copyright (c) 2009 Andrew Nutter-Upham
|
96
|
+
|
97
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
98
|
+
a copy of this software and associated documentation files (the
|
99
|
+
'Software'), to deal in the Software without restriction, including
|
100
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
101
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
102
|
+
permit persons to whom the Software is furnished to do so, subject to
|
103
|
+
the following conditions:
|
104
|
+
|
105
|
+
The above copyright notice and this permission notice shall be
|
106
|
+
included in all copies or substantial portions of the Software.
|
107
|
+
|
108
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
109
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
110
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
111
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
112
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
113
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
114
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/lib/mydbi.rb
ADDED
@@ -0,0 +1,170 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# A simplified dbi api that uses defaults.
|
4
|
+
#
|
5
|
+
# It isn't that the dbi syntax is difficult or too verbose
|
6
|
+
# but the patterns of getting the last_insert_id for inserts,
|
7
|
+
# the number of rows modified for updates, and looping over
|
8
|
+
# the rows from a select are so common that having a single
|
9
|
+
# shorthand, one that can use default to my local database
|
10
|
+
# connection is handy.
|
11
|
+
#
|
12
|
+
require 'dbi'
|
13
|
+
require 'yamlrc'
|
14
|
+
|
15
|
+
class Mydbi
|
16
|
+
VERSION = "1.0.5"
|
17
|
+
end
|
18
|
+
|
19
|
+
$mydbi_db = nil;
|
20
|
+
|
21
|
+
$mydbi_config = {
|
22
|
+
:default => {
|
23
|
+
:name => '',
|
24
|
+
:host => 'localhost',
|
25
|
+
:username => 'root',
|
26
|
+
:password => ''
|
27
|
+
}
|
28
|
+
}.merge(Yamlrc.load(".mydbirc"))
|
29
|
+
|
30
|
+
# make a connection, returns the dbh
|
31
|
+
#
|
32
|
+
# the top level query and ascii_query commands will work with
|
33
|
+
# the most recent dbconnection. but if you save the DatabaseHandle
|
34
|
+
# returned you can still use those methods off of that object.
|
35
|
+
#
|
36
|
+
# @db = dbconnect
|
37
|
+
# @db.query("whatever")
|
38
|
+
#
|
39
|
+
def dbconnect(db=nil, host=nil, username=nil, password=nil)
|
40
|
+
config = $mydbi_config[:default]
|
41
|
+
|
42
|
+
# optionally reset the config to a named datasource
|
43
|
+
if db.instance_of?(Symbol)
|
44
|
+
if $mydbi_config.key?(db)
|
45
|
+
config = $mydbi_config[db]
|
46
|
+
db = nil
|
47
|
+
else
|
48
|
+
throw ArgumentError.new("No database connection named ':#{db}' is configured")
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
db = config[:db] if db.nil?
|
53
|
+
host = config[:host] if host.nil?
|
54
|
+
username = config[:username] if username.nil?
|
55
|
+
password = config[:password] if password.nil?
|
56
|
+
|
57
|
+
$mydbi_db = DBI.connect("DBI:Mysql:#{db}:#{host}", username, password)
|
58
|
+
end
|
59
|
+
|
60
|
+
# see DBI::DatabaseHandle's query
|
61
|
+
def query(sql,*values)
|
62
|
+
return $mydbi_db.query(sql,*values) # TODO this is not passing the block along properly
|
63
|
+
end
|
64
|
+
|
65
|
+
# see DBI::DatabaseHandle's ascii_query
|
66
|
+
def ascii_query(sql,*values)
|
67
|
+
return $mydbi_db.ascii_query(sql,*values)
|
68
|
+
end
|
69
|
+
|
70
|
+
module DBI
|
71
|
+
class DatabaseHandle
|
72
|
+
|
73
|
+
# execute a query
|
74
|
+
#
|
75
|
+
# SELECT:
|
76
|
+
#
|
77
|
+
# Either the StatementHandle (sth) is returned or if you
|
78
|
+
# pass it a block it will iterate across the results
|
79
|
+
# yielding the row
|
80
|
+
#
|
81
|
+
# sth = query("select * from songs")
|
82
|
+
# puts sth.rows
|
83
|
+
# while( row = sth.fetch )
|
84
|
+
# p row
|
85
|
+
# end
|
86
|
+
# sth.finish
|
87
|
+
#
|
88
|
+
# or
|
89
|
+
#
|
90
|
+
# query("select * from songs") do |row|
|
91
|
+
# p row
|
92
|
+
# end
|
93
|
+
#
|
94
|
+
# INSERT:
|
95
|
+
#
|
96
|
+
# Will return the last_insert_id. Warning! If you provide a bulk insert you'll only
|
97
|
+
# see get back the id of the first insert (with Mysql 5.0.45-Debian_1ubuntu3-log anyway).
|
98
|
+
#
|
99
|
+
# last_insert_id = query("insert into songs values (?,?,?,?)",nil,artist,album,song)
|
100
|
+
# => 1
|
101
|
+
#
|
102
|
+
# last_insert_id = query("insert into songs values (?,?,?,?)",nil,artist,album,song)
|
103
|
+
# => 2
|
104
|
+
#
|
105
|
+
#
|
106
|
+
# UPDATE:
|
107
|
+
#
|
108
|
+
# Will return the affected_rows_count
|
109
|
+
#
|
110
|
+
# affected_row_count = query("update songs set artist=? where song_id = ?",new_artist, song_id)
|
111
|
+
# => 1
|
112
|
+
#
|
113
|
+
# default:
|
114
|
+
#
|
115
|
+
# returns sth after preparing and executing
|
116
|
+
def query(sql,*values)
|
117
|
+
case sql
|
118
|
+
when /^\s*select/i
|
119
|
+
sth = self.prepare(sql)
|
120
|
+
sth.execute(*values)
|
121
|
+
if block_given?
|
122
|
+
while row = sth.fetch do
|
123
|
+
yield(row)
|
124
|
+
end
|
125
|
+
sth.finish
|
126
|
+
else
|
127
|
+
return sth
|
128
|
+
end
|
129
|
+
|
130
|
+
when /^\s*update/i
|
131
|
+
return self.do(sql,*values); # returns affected_rows_count
|
132
|
+
|
133
|
+
when /^\s*insert/i
|
134
|
+
# automatically getting the last_insert id is really only meant
|
135
|
+
# to work when inserting a single record. bulk inserts ?!
|
136
|
+
rows_inserted = self.do(sql,*values);
|
137
|
+
last_id = nil
|
138
|
+
sql.squeeze(" ").match(/insert into ([^ ]*) /) # grab the table
|
139
|
+
query("select last_insert_id() from #{$1} limit 1"){|row|
|
140
|
+
last_id = row[0];
|
141
|
+
}
|
142
|
+
return last_id
|
143
|
+
|
144
|
+
else # create, drop, truncate, show, ...
|
145
|
+
sth = self.prepare(sql)
|
146
|
+
sth.execute(*values)
|
147
|
+
return sth
|
148
|
+
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
# prints the query results (columns names and values)
|
153
|
+
# much like the mysql commandline
|
154
|
+
#
|
155
|
+
# +----+---------+---------------------+
|
156
|
+
# | id | song_id | played_at |
|
157
|
+
# +----+---------+---------------------+
|
158
|
+
# | 3 | 713 | 2007-12-01 00:44:44 |
|
159
|
+
# | 4 | 174 | 2007-12-01 00:44:44 |
|
160
|
+
# +----+---------+---------------------+
|
161
|
+
def ascii_query(sql,*values)
|
162
|
+
sth = self.query(sql,*values)
|
163
|
+
rows = sth.fetch_all
|
164
|
+
col_names = sth.column_names
|
165
|
+
sth.finish
|
166
|
+
DBI::Utils::TableFormatter.ascii(col_names, rows)
|
167
|
+
end
|
168
|
+
|
169
|
+
end
|
170
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mydbi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.5
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Nutter-Upham
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-19 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: dbi
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: dbd-mysql
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: yamlrc
|
37
|
+
type: :runtime
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
- !ruby/object:Gem::Dependency
|
46
|
+
name: hoe
|
47
|
+
type: :development
|
48
|
+
version_requirement:
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.3.3
|
54
|
+
version:
|
55
|
+
description: |-
|
56
|
+
A simple wrapper for dbi that provides two simple functions dbconnect(profile) and query(sql),
|
57
|
+
along with auth profiles via a configuration file.
|
58
|
+
email:
|
59
|
+
- andynu+mydbi@gmail.com
|
60
|
+
executables: []
|
61
|
+
|
62
|
+
extensions: []
|
63
|
+
|
64
|
+
extra_rdoc_files:
|
65
|
+
- History.txt
|
66
|
+
- Manifest.txt
|
67
|
+
- README.txt
|
68
|
+
files:
|
69
|
+
- History.txt
|
70
|
+
- Manifest.txt
|
71
|
+
- README.txt
|
72
|
+
- Rakefile
|
73
|
+
- lib/mydbi.rb
|
74
|
+
has_rdoc: true
|
75
|
+
homepage: http://github.com/andynu/mydbi
|
76
|
+
licenses: []
|
77
|
+
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options:
|
80
|
+
- --main
|
81
|
+
- README.txt
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: "0"
|
89
|
+
version:
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: "0"
|
95
|
+
version:
|
96
|
+
requirements: []
|
97
|
+
|
98
|
+
rubyforge_project: mydbi
|
99
|
+
rubygems_version: 1.3.5
|
100
|
+
signing_key:
|
101
|
+
specification_version: 3
|
102
|
+
summary: A simple wrapper for dbi that provides two simple functions dbconnect(profile) and query(sql), along with auth profiles via a configuration file.
|
103
|
+
test_files: []
|
104
|
+
|