sqlyzer 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.
@@ -0,0 +1,151 @@
1
+ # SQLYZER Ruby Object serializer to SQL.
2
+ # For more information visit http://sqlyzer.rubyforge.org
3
+ #
4
+ # This program is free software; you can redistribute it and/or
5
+ # modify it under the terms of the GNU General Public License
6
+ # as published by the Free Software Foundation; either version 2
7
+ # of the License, or (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program; if not, write to the Free Software
16
+ # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
+ #
18
+
19
+ require 'sqlyzer/errors'
20
+ require 'sqlyzer/create'
21
+ require 'sqlyzer/parameters'
22
+
23
+ module Sqlyzer
24
+
25
+ module Sql
26
+
27
+ #
28
+ #Handle generation of all Sql requests that a Sqlyzer::Serializer could ask
29
+ #for :
30
+ #* sql_select,
31
+ #* sql_insert,
32
+ #* sql_update,
33
+ #* sql_delete,
34
+ #* sql_table.
35
+ #
36
+ module Request
37
+ include Create
38
+
39
+ private
40
+
41
+ #
42
+ #Run given block code and store return value. The return value is considered
43
+ #as a format which will be applied on _keys_ and _values_ Sqlyzer::Parameter::List
44
+ #in a new or redefined method called _symbol_.
45
+ #This method call permit to generate the format only once.
46
+ #
47
+ def _sql_request(symbol, keys, values = false)
48
+ fmt = yield
49
+ if values
50
+ self.class.module_eval <<-"end_eval"
51
+ def #{symbol}(table, keys, values)
52
+ sprintf("#{fmt}", *(values.to_sql(self) + keys.to_sql(self)))
53
+ end
54
+ end_eval
55
+ sprintf(fmt, *(values.to_sql(self) + keys.to_sql(self)))
56
+ else
57
+ self.class.module_eval <<-"end_eval"
58
+ def #{symbol}(table, keys, values = false)
59
+ sprintf("#{fmt}", *keys.to_sql(self))
60
+ end
61
+ end_eval
62
+ sprintf(fmt, *keys.to_sql(self))
63
+ end
64
+ end
65
+
66
+ #
67
+ #Proxy method to apply Sqlyzer::Parameter::List join method for each member
68
+ #of _datas_ array with _suffix_ string.
69
+ #
70
+ def _sql_data_join(suffix, *datas)
71
+ res = ''
72
+ for list in datas
73
+ if res.empty?
74
+ res = list.join(', ', suffix)
75
+ else
76
+ tmp = list.join(', ', suffix)
77
+ res += ', ' + tmp unless tmp.empty?
78
+ end
79
+ end
80
+ res
81
+ end
82
+
83
+ protected
84
+
85
+ #
86
+ #Generate a Sql SELECT command on table called _table_ with Sqlyzer::Parameter::List
87
+ #_keys_ as primary keys and _values_ as regular fields.
88
+ #Command format will be generated once thanks to _sql_request.
89
+ #
90
+ def sql_select(table, keys, values)
91
+ _sql_request(:sql_select, keys) {
92
+ "SELECT #{_sql_data_join('', values)} " +
93
+ "FROM #{table} " +
94
+ "WHERE #{keys.join ' AND ', ' = %s'};"
95
+ }
96
+ end
97
+
98
+ #
99
+ #Generate a Sql INSERT command on table called _table_ with Sqlyzer::Parameter::List
100
+ #_keys_ as primary keys and _values_ as regular fields.
101
+ #Command format will be generated once thanks to _sql_request.
102
+ #
103
+ def sql_insert(table, keys, values)
104
+ _sql_request(:sql_insert, keys, values) {
105
+ "INSERT INTO #{table} (" +
106
+ _sql_data_join('', values, keys) +
107
+ ") VALUES (#{'%s, ' * (values.length + keys.length - 1)}%s);"
108
+ }
109
+ end
110
+
111
+ #
112
+ #Generate a Sql UPDATE command on table called _table_ with Sqlyzer::Parameter::List
113
+ #_keys_ as primary keys and _values_ as regular fields.
114
+ #Command format will be generated once thanks to _sql_request.
115
+ #
116
+ def sql_update(table, keys, values)
117
+ _sql_request(:sql_update, keys, values) {
118
+ "UPDATE #{table} SET " +
119
+ _sql_data_join(' = %s', values) +
120
+ " WHERE #{keys.join ' AND ', ' = %s'};"
121
+ }
122
+ end
123
+
124
+ #
125
+ #Generate a Sql DELETE command on table called _table_ with Sqlyzer::Parameter::List
126
+ #_keys_ as primary keys and _values_ as regular fields.
127
+ #Command format will be generated once thanks to _sql_request.
128
+ #
129
+ def sql_delete(table, keys, values = false)
130
+ _sql_request(:sql_delete, keys) {
131
+ "DELETE * FROM #{table} " +
132
+ "WHERE #{keys.join ' AND ', ' = %s'};"
133
+ }
134
+ end
135
+
136
+ #
137
+ #Generate a Sql table called _table_ with Sqlyzer::Parameter::List
138
+ #_keys_ as primary keys and _values_ as regular fields.
139
+ #Unique index will be created on _keys_.
140
+ #
141
+ def sql_table(table, keys, values)
142
+ sql_create_table(table, keys, values) +
143
+ sql_create_table_index!(table, keys) +
144
+ sql_alter_table_pk(table, keys)
145
+ end
146
+
147
+ end #~ Request
148
+
149
+ end #~ Sql
150
+
151
+ end #~ Sqlyzer
@@ -0,0 +1,200 @@
1
+ # SQLYZER Ruby Object serializer to SQL.
2
+ # For more information visit http://sqlyzer.rubyforge.org
3
+ #
4
+ # This program is free software; you can redistribute it and/or
5
+ # modify it under the terms of the GNU General Public License
6
+ # as published by the Free Software Foundation; either version 2
7
+ # of the License, or (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program; if not, write to the Free Software
16
+ # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
+ #
18
+ require 'sqlyzer/errors'
19
+ require 'sqlyzer/request'
20
+ require 'sqlyzer/container'
21
+ require 'sqlyzer/parameters'
22
+ require 'sqlyzer/handler'
23
+
24
+ module Sqlyzer
25
+
26
+ #
27
+ #Permit to any Ruby object to be serialized on unserialized to an automatically
28
+ #generated Sql table through following methods :
29
+ #* sql_new,
30
+ #* sql_load,
31
+ #* sql_save,
32
+ #* sql_dispose.
33
+ #
34
+ module Serializer
35
+ include Container
36
+ include Sql::Request
37
+ include Parameter::Types::Alias
38
+
39
+ public
40
+
41
+ #
42
+ #Request generation of _action_ Sql request for current object.
43
+ #
44
+ def sql_serialize(action)
45
+ method(action).call(
46
+ sql_container_table,
47
+ sql_container_keys,
48
+ sql_container_values
49
+ )
50
+ end
51
+ alias to_sql sql_serialize
52
+
53
+ public
54
+
55
+ #
56
+ #Try to load the object from associated table through a SELECT command.
57
+ #If command failed, insert a new row in the table through an INSERT command.
58
+ #
59
+ #See Sqlyzer::Request.
60
+ #
61
+ def sql_new
62
+ begin
63
+ Handler::request self, :sql_select
64
+ rescue Sqlyzer::RequestError
65
+ Handler::request self, :sql_insert
66
+ false
67
+ else
68
+ true
69
+ end
70
+ end
71
+
72
+ #
73
+ #Load the object from associated table through a SELECT command.
74
+ #
75
+ #See Sqlyzer::Request.
76
+ #
77
+ def sql_load
78
+ Handler::request self, :sql_select
79
+ end
80
+
81
+ #
82
+ #Try to update the object on associated table through an UPDATE command.
83
+ #If command failed, insert a new row in the table through an INSERT command.
84
+ #
85
+ #See Sqlyzer::Request.
86
+ #
87
+ def sql_save
88
+ begin
89
+ Handler::request self, :sql_update
90
+ rescue Sqlyzer::RequestError
91
+ Handler::request self, :sql_insert
92
+ false
93
+ else
94
+ true
95
+ end
96
+ end
97
+
98
+ #
99
+ #Delete the object from associated table through a DELETE command.
100
+ #
101
+ #See Sqlyzer::Request.
102
+ #
103
+ def sql_dispose
104
+ Handler::request self, :sql_delete
105
+ end
106
+
107
+ public
108
+
109
+ #
110
+ #Methods to add on extended class.
111
+ #
112
+ module Serializer_Class
113
+
114
+ private
115
+
116
+ #
117
+ #Declare an Sql attibute for each _data_, alias to attr_accessor.
118
+ #This method will assert that each _data_ is a kind of Sqlyzer::Parameter::Default.
119
+ #
120
+ def sql_attr(*data)
121
+ for id in data
122
+ if id.kind_of?(Parameter::Default)
123
+ attr_accessor id.symbol
124
+ else
125
+ attr_accessor id
126
+ end
127
+ end
128
+ end
129
+
130
+ #
131
+ #Declare each _data_ as a primary key of future Sql table.
132
+ #Call sql_attr on _data_.
133
+ #
134
+ def sql_has_keys(*data)
135
+ sql_attr(*data)
136
+ extend Sqlyzer::Sql::Request
137
+ Sqlyzer::Container::sql_has_keys self, data
138
+ end
139
+
140
+ #
141
+ #Declare each _data_ as a regular field of future Sql table.
142
+ #Call sql_attr on _data_.
143
+ #
144
+ def sql_has_values(*data)
145
+ sql_attr(*data)
146
+ extend Sqlyzer::Sql::Request
147
+ Sqlyzer::Container::sql_has_values self, data
148
+ end
149
+
150
+ #
151
+ #Set wished _name_ for future Sql table.
152
+ #
153
+ def sql_use_table(name)
154
+ Sqlyzer::Container::sql_set_table self, name
155
+ end
156
+
157
+ #
158
+ #Shortcut to :
159
+ #1. sql_use_table self.class.name
160
+ #2. sql_has_keys _key_
161
+ #3. sql_has_values _values_
162
+ #
163
+ #First step will be ignored if table has already a name set.
164
+ #
165
+ def sql_has(key, *values)
166
+ sql_use_table self.inspect.gsub('::', '_') unless
167
+ self.const_defined?(:SQL_CONTAINER_TABLE)
168
+ sql_has_keys key
169
+ sql_has_values(*values)
170
+ end
171
+
172
+ def sql_has_many(key, klass)
173
+ require 'sqlyzer/associate'
174
+ include Sqlyzer::Associate::One unless
175
+ include?(Sqlyzer::Associate::One)
176
+ Sqlyzer::Associate::One::link self, key, klass
177
+ end
178
+
179
+ def sql_has_one(key, klass)
180
+ require 'sqlyzer/associate'
181
+ include Sqlyzer::Associate::One unless
182
+ include?(Sqlyzer::Associate::One)
183
+ Sqlyzer::Associate::One::link self, key, klass
184
+ end
185
+
186
+ end #~ Serializer_Class
187
+
188
+ #
189
+ #Extend the class will Serializer_Class methods.
190
+ #
191
+ class << self
192
+ def included(klass)
193
+ super
194
+ klass.__send__(:extend, Serializer_Class)
195
+ end
196
+ end
197
+
198
+ end #~ Serializer
199
+
200
+ end #~ Sqlyzer
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: sqlyzer
5
+ version: !ruby/object:Gem::Version
6
+ version: "0.1"
7
+ date: 2007-05-02 00:00:00 +02:00
8
+ summary: A complete Ruby Object to Sql serialize solution. Generate automatically all needed Sql objects without any of Sql from the user.
9
+ require_paths:
10
+ - lib
11
+ email: paul.quemin@gmail.com
12
+ homepage: http://sqlyzer.rubyforge.org
13
+ rubyforge_project: sqlyzer
14
+ description: Sqlyzer is a Ruby Mixin able to store, load, update and delete Ruby objects and their relationships in a database by automatically generating adapted tables and queries without a line of Sql.
15
+ autorequire: sqlyzer
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ - |
29
+ -----BEGIN CERTIFICATE-----
30
+ MIIDODCCAiCgAwIBAgIBADANBgkqhkiG9w0BAQUFADBCMRQwEgYDVQQDDAtwYXVs
31
+ LnF1ZW1pbjEVMBMGCgmSJomT8ixkARkWBWdtYWlsMRMwEQYKCZImiZPyLGQBGRYD
32
+ Y29tMB4XDTA3MDUxMjAwNTkzNloXDTA4MDUxMTAwNTkzNlowQjEUMBIGA1UEAwwL
33
+ cGF1bC5xdWVtaW4xFTATBgoJkiaJk/IsZAEZFgVnbWFpbDETMBEGCgmSJomT8ixk
34
+ ARkWA2NvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMGz6qvavTSN
35
+ LXXZx93o2o0s6GpwO5CIHe5U05kl2Bp116PObKipguxHkGqXliXgNQztJun/IIdV
36
+ Tglre5AgraCA9rQregB8tYhhKcsbZ/MnpyYKKpelxtgVW3zELUe+fcvdzNLmF2rP
37
+ Q6vuqfkfsmC80OQlcwp9PBXHOLP9XEjl6Ea+vHEvWzJT3h2DhzKoaKqrsb7Gq6hw
38
+ AQphUzFSPy80IW/LXGXFg8XtxbfkK8itgXtOmwyeSb0ip7NLhPLoJYBHChwQmhmW
39
+ xZD0E5HJ5p6yD5K0qv4jkRGoC5mCqnLKHw7dGTWhNFWqoxvNGtuj0jzIulRH2BFb
40
+ 2FkwSIvxV9cCAwEAAaM5MDcwCQYDVR0TBAIwADALBgNVHQ8EBAMCBLAwHQYDVR0O
41
+ BBYEFA5dDDK/hmhCKCDfw41ILZpXh4MCMA0GCSqGSIb3DQEBBQUAA4IBAQA86iar
42
+ kNqfVFBv7IEsHGBGEmKivDNFRgibyj7O8RzR/QkGuSGeFBEzPs34r/MeqT1FJDze
43
+ XKbyL9EMQQMQk6nwgrQVTt/M4Ba4VgW+BLG4yKWg4ovgCTNsXevup9YJRKk0Y6Mr
44
+ HhmDFlmlq28v5ZZSMr64XPi9L4QH6uYhkelW6qdmKIDLHNfYZXHWmkO69SMmjBvI
45
+ VuuPJbK5CU0A7Ojmttp++pBvTv1VXLFi7LsjaQ4MXR5PmbwHW1hFS8XLYIrntFBZ
46
+ prHBm/3lDy7y47+VxD3wqJdQLfq3AhEjdduNdFLSWBsonIPV4J9skJhn8LioRk8J
47
+ 6nMtd6SBoidhjHqA
48
+ -----END CERTIFICATE-----
49
+
50
+ post_install_message:
51
+ authors:
52
+ - Paul Quemin
53
+ files:
54
+ - COPYING.txt
55
+ - README.txt
56
+ - lib/sqlyzer.rb
57
+ - lib/sqlyzer/associate.rb
58
+ - lib/sqlyzer/create.rb
59
+ - lib/sqlyzer/errors.rb
60
+ - lib/sqlyzer/parameters.rb
61
+ - lib/sqlyzer/serializer.rb
62
+ - lib/sqlyzer/container.rb
63
+ - lib/sqlyzer/db.rb
64
+ - lib/sqlyzer/handler.rb
65
+ - lib/sqlyzer/request.rb
66
+ test_files: []
67
+
68
+ rdoc_options: []
69
+
70
+ extra_rdoc_files:
71
+ - COPYING.txt
72
+ - README.txt
73
+ executables: []
74
+
75
+ extensions: []
76
+
77
+ requirements: []
78
+
79
+ dependencies: []
80
+
metadata.gz.sig ADDED
Binary file