sqlite2dbf 0.1.8 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/logging.rb CHANGED
@@ -54,140 +54,140 @@ class TClass
54
54
  end
55
55
  =end
56
56
  module Logging
57
- include File_Checking
58
-
59
- @@have_log = false
60
- @@LOG_CONF = File.dirname(File.absolute_path(__FILE__)) << File::Separator << 'log.conf'
61
-
62
- # Call this method in an instance-method (e.g. initialize() ) to define the
63
- # object-level logger; i.e. an object-specific member @log.
64
- # Call this method within the class-definition for a class-level logger; i.e.
65
- # a member @log for class-level acces.
66
- # The method returns the logger, so you can actually do what you want with it.
67
- def init_logger(target = STDOUT, level = Logger::INFO)
68
- # Prepare for a class-level logger. This is actually quite cool.
69
-
70
- # ---> Ingeniuous code starts here
71
- cn = (self.class == Class ? name : self.class.name)
72
- # <--- Ingeniuous code ends here
73
-
74
- # allow to override the set log-levels with an
75
- # external configuration (log.conf).
76
- log_conf(cn)
77
- # Or use the defaults as set here or elsewhere...
78
-
79
- @level ||= level
80
- @target ||= target
81
-
82
- @log = Logger.new(@target)
83
- @log.level = @level
84
-
85
- @log.formatter = proc do |severity, datetime, progname, msg|
86
- t = Time.now
87
- "#{cn}: #{severity} #{t.hour}-#{t.min}-#{t.sec}: #{msg}\n"
88
- end
89
- if ! @@have_log
90
- @log.debug cn.dup << ' reading logging-configuration from ' << @@LOG_CONF
91
- @@have_log = true
92
- @log.debug('level is ' << level.to_s)
93
- end
94
- return @log
95
- end
96
-
97
- # Set the log-target to an IO object.
98
- def log_target=(target)
99
- @target = target
100
- @log = Logger.new(@@target)
101
- @log.level = @level
102
- end
103
-
104
- # set the log-level
105
- def log_level=(level)
106
- @level = level
107
- @log.level = @level
108
- end
109
-
110
- private
111
-
112
- # Override or set the log-level and target-device, as set in a file 'log.conf'.
113
- # I do not like the look of this, but it works just the way I want it to.
114
- # "HEAVANS! Isn't there a standard way to do this in Ruby, for Christ's sake?", you say.
115
- # Heck, I don't care. <= Read that again, I say.
116
- def log_conf(cn = nil)
117
- config = level = target = nil
118
- # puts 'log-config is in ' << @@LOG_CONF
119
- if(File::exist?(@@LOG_CONF) )
120
- begin
121
- conf = File.read(@@LOG_CONF)
122
- config = instance_eval(conf)
123
- rescue Exception => ex
124
- STDERR.puts "WARNING! Cannot evaluate the logger-configuration!" << ' ' << ex.message
125
- STDERR.puts "Default log-levels apply."
126
- end
127
- else
128
- puts "Default log-levels apply."
129
- end
130
-
131
- if(config && config.respond_to?(:to_hash) )
132
- config.default = nil
133
- if cn
134
- config = config[cn.to_sym]
135
- else
136
- config = config[self.class.name.to_sym]
137
- end
138
-
139
- if(config )
140
- if(config.respond_to?(:to_ary) && config.size == 2)
141
- @level, @target = config
142
- @target.downcase!
143
- logdir = File.dirname(@target)
144
- msg = file_check(logdir, :exist?, :directory?, :writable?)
145
- if(msg)
146
- STDERR.puts "WARNING! A logfile for '%s' cannot be written to %s (%s)!" %[self.class.name, logdir, msg]
147
- @target = nil
148
- end
149
- else
150
- @level = config
151
- end
152
- end
153
- end
154
- end
57
+ include File_Checking
58
+
59
+ @@have_log = false
60
+ @@LOG_CONF = File.dirname(File.absolute_path(__FILE__)) << File::Separator << 'log.conf'
61
+
62
+ # Call this method in an instance-method (e.g. initialize() ) to define the
63
+ # object-level logger; i.e. an object-specific member @log.
64
+ # Call this method within the class-definition for a class-level logger; i.e.
65
+ # a member @log for class-level acces.
66
+ # The method returns the logger, so you can actually do what you want with it.
67
+ def init_logger(target = STDOUT, level = Logger::INFO)
68
+ # Prepare for a class-level logger. This is actually quite cool.
69
+
70
+ # ---> Ingeniuous code starts here
71
+ cn = (self.class == Class ? name : self.class.name)
72
+ # <--- Ingeniuous code ends here
73
+
74
+ # allow to override the set log-levels with an
75
+ # external configuration (log.conf).
76
+ log_conf(cn)
77
+ # Or use the defaults as set here or elsewhere...
78
+
79
+ @level ||= level
80
+ @target ||= target
81
+
82
+ @log = Logger.new(@target)
83
+ @log.level = @level
84
+
85
+ @log.formatter = proc do |severity, datetime, progname, msg|
86
+ t = Time.now
87
+ "#{cn}: #{severity} #{t.hour}-#{t.min}-#{t.sec}: #{msg}\n"
88
+ end
89
+ if ! @@have_log
90
+ @log.debug cn.dup << ' reading logging-configuration from ' << @@LOG_CONF
91
+ @@have_log = true
92
+ @log.debug('level is ' << level.to_s)
93
+ end
94
+ return @log
95
+ end
96
+
97
+ # Set the log-target to an IO object.
98
+ def log_target=(target)
99
+ @target = target
100
+ @log = Logger.new(@@target)
101
+ @log.level = @level
102
+ end
103
+
104
+ # set the log-level
105
+ def log_level=(level)
106
+ @level = level
107
+ @log.level = @level
108
+ end
109
+
110
+ private
111
+
112
+ # Override or set the log-level and target-device, as set in a file 'log.conf'.
113
+ # I do not like the look of this, but it works just the way I want it to.
114
+ # "HEAVANS! Isn't there a standard way to do this in Ruby, for Christ's sake?", you say.
115
+ # Heck, I don't care. <= Read that again, I say.
116
+ def log_conf(cn = nil)
117
+ config = level = target = nil
118
+ # puts 'log-config is in ' << @@LOG_CONF
119
+ if(File::exist?(@@LOG_CONF) )
120
+ begin
121
+ conf = File.read(@@LOG_CONF)
122
+ config = instance_eval(conf)
123
+ rescue Exception => ex
124
+ STDERR.puts "WARNING! Cannot evaluate the logger-configuration!" << ' ' << ex.message
125
+ STDERR.puts "Default log-levels apply."
126
+ end
127
+ else
128
+ puts "Default log-levels apply."
129
+ end
130
+
131
+ if(config && config.respond_to?(:to_hash) )
132
+ config.default = nil
133
+ if cn
134
+ config = config[cn.to_sym]
135
+ else
136
+ config = config[self.class.name.to_sym]
137
+ end
138
+
139
+ if(config )
140
+ if(config.respond_to?(:to_ary) && config.size == 2)
141
+ @level, @target = config
142
+ @target.downcase!
143
+ logdir = File.dirname(@target)
144
+ msg = file_check(logdir, :exist?, :directory?, :writable?)
145
+ if(msg)
146
+ STDERR.puts "WARNING! A logfile for '%s' cannot be written to %s (%s)!" %[self.class.name, logdir, msg]
147
+ @target = nil
148
+ end
149
+ else
150
+ @level = config
151
+ end
152
+ end
153
+ end
154
+ end
155
155
  end
156
156
 
157
157
  ######### test
158
158
  if __FILE__ == $0
159
- class TClass
160
- # class level ---->
161
- self.extend(Logging)
162
- @@log = init_logger(STDOUT, Logger::INFO)
163
- # <------
164
- # object-level ---->
165
- include Logging
166
- # <---------
167
-
168
- def test_log
169
- @@log.info('class-level logger called from instance: ' << @@log.to_s)
170
- #@log = @@log # works too
171
- @log = TClass.class_eval{@log}
172
- @log.info('AGAIN: class-level logger called from instance: ' << @log.to_s)
173
- @log.debug("you won't see this on log-level INFO")
174
-
175
- # object-level ---->
176
- init_logger
177
- # <-----------
178
- @log.info("That's a different thing: " << @log.to_s << " - object-level logger!")
179
-
180
- end
181
- def self::test_log
182
- @log.info('class-level logger called from class: ' << @log.to_s)
183
- @@log.info('AGAIN: class-level logger called from class: ' << @log.to_s)
184
- end
185
- end
186
-
187
- TClass.new.test_log # class-logger + 1st object-logger
188
- TClass.new.test_log # same class-logger + 2nd object-logger
189
-
190
- TClass::test_log # same class-logger
191
- puts 'And just say it once clearly: THIS IS COOOL!!'
159
+ class TClass
160
+ # class level ---->
161
+ self.extend(Logging)
162
+ @@log = init_logger(STDOUT, Logger::INFO)
163
+ # <------
164
+ # object-level ---->
165
+ include Logging
166
+ # <---------
167
+
168
+ def test_log
169
+ @@log.info('class-level logger called from instance: ' << @@log.to_s)
170
+ #@log = @@log # works too
171
+ @log = TClass.class_eval{@log}
172
+ @log.info('AGAIN: class-level logger called from instance: ' << @log.to_s)
173
+ @log.debug("you won't see this on log-level INFO")
174
+
175
+ # object-level ---->
176
+ init_logger
177
+ # <-----------
178
+ @log.info("That's a different thing: " << @log.to_s << " - object-level logger!")
179
+
180
+ end
181
+ def self::test_log
182
+ @log.info('class-level logger called from class: ' << @log.to_s)
183
+ @@log.info('AGAIN: class-level logger called from class: ' << @log.to_s)
184
+ end
185
+ end
186
+
187
+ TClass.new.test_log # class-logger + 1st object-logger
188
+ TClass.new.test_log # same class-logger + 2nd object-logger
189
+
190
+ TClass::test_log # same class-logger
191
+ puts 'And just say it once clearly: THIS IS COOOL!!'
192
192
  end
193
193
  #EOF
data/lib/mapping.rb ADDED
@@ -0,0 +1,94 @@
1
+ #encoding: UTF-8
2
+ =begin
3
+ /***************************************************************************
4
+ * ©2016 Michael Uplawski <michael.uplawski@uplawski.eu> *
5
+ * *
6
+ * This program is free software; you can redistribute it and/or modify *
7
+ * it under the terms of the GNU General Public License as published by *
8
+ * the Free Software Foundation; either version 3 of the License, or *
9
+ * (at your option) any later version. *
10
+ * *
11
+ * This program is distributed in the hope that it will be useful, *
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
+ * GNU General Public License for more details. *
15
+ * *
16
+ * You should have received a copy of the GNU General Public License *
17
+ * along with this program; if not, write to the *
18
+ * Free Software Foundation, Inc., *
19
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20
+ ***************************************************************************/
21
+ =end
22
+
23
+ # This is not a module.
24
+ # TODO get rid of the ambivalence.
25
+ # This class represents the mapping between SQLite data-types and those in
26
+ # dBase. It is basically a decorator for an Array of OpenStruct objects.
27
+ # Mapping-objects have access to the configuration.
28
+ # TODO: Decide if this needs to be a singleton (one table only) or if it might,
29
+ # in the future, make sense to preview several mapping-objects for the
30
+ # transformation of several tables in a row.
31
+ class Mapping
32
+
33
+ include Logging
34
+
35
+ @@TMap = {
36
+ 'INT' => 1,
37
+ 'TINYINT' => 1,
38
+ 'SMALLINT' => 1,
39
+ 'MEDIUMINT' => 1,
40
+ 'BIGINT' => 1,
41
+ 'UNSIGNED BIGINT' => 1,
42
+ 'VARCHAR' => 0,
43
+ 'CHARACTER' => 0,
44
+ 'VARYING CHARACTER' => 0,
45
+ 'LONGVARCHAR' => 0,
46
+ 'TEXT' => 0,
47
+ 'BLOB' => 0,
48
+ 'INTEGER' => 1,
49
+ 'FLOAT' => 2,
50
+ 'REAL' => 2,
51
+ 'DOUBLE' => 2
52
+ }
53
+
54
+ # Constructor
55
+ # Creates a mapping for the table of name 'table'
56
+ # and basedon the table_info.
57
+ def initialize config, table_info, content
58
+ init_logger()
59
+
60
+ @table_name = config.name
61
+ @mapping = []
62
+ @content = content
63
+ @date_fields = config.date ? config.date : []
64
+ @time_fields = config.time ? config.time : []
65
+
66
+ table_info.each_with_index do |field, i|
67
+ @log.debug('field is ' << (field ? field.to_s : ' N I L ') )
68
+ field_name = field['name']
69
+
70
+ ftype = nil
71
+
72
+ # Enforce Text field for dates and times.
73
+ ftype = 0 if @date_fields.include?(field_name)
74
+ ftype = 0 if @time_fields.include?(field_name)
75
+
76
+ # Do as you must for the remainder
77
+ ftype ||= @@TMap.detect{|key, value| field['type'].upcase.start_with?(key) }[1]
78
+
79
+ @mapping << OpenStruct.new(:name => field_name, :type => ftype, :index => i)
80
+ end
81
+ end
82
+ def [](*args)
83
+ @mapping.detect do |field|
84
+ field.index == args[0]
85
+ end
86
+ end
87
+
88
+ def each(&b)
89
+ @mapping.each do |field|
90
+ yield(field)
91
+ end
92
+ end
93
+
94
+ end