gorm 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/gorm.rb +15 -10
- data/lib/gorm/schema.rb +4 -7
- data/lib/gorm/table.rb +3 -5
- data/lib/gorm/version.rb +5 -0
- metadata +7 -13
- data/bin/gorm +0 -72
- data/lib/gorm_version.rb +0 -3
- data/templates/example_database.rb +0 -12
- data/templates/schema.cpp.erb +0 -124
- data/templates/schema.h.erb +0 -76
- data/templates/table.cpp.erb +0 -0
- data/templates/table.h.erb +0 -0
data/lib/gorm.rb
CHANGED
@@ -1,30 +1,35 @@
|
|
1
|
+
$LOAD_PATH << File.join(File.dirname(__FILE__))
|
1
2
|
require 'gorm/schema'
|
3
|
+
require 'gorm/version'
|
4
|
+
require 'cog'
|
2
5
|
|
6
|
+
# Inject this tools templates onto the cog template lookup path
|
7
|
+
# Do not remove this line
|
8
|
+
Cog::Config.instance.tool_templates_path = File.expand_path(File.join(__FILE__, '..', '..', 'cog', 'templates'))
|
9
|
+
|
10
|
+
# Set the template which is used to create generators for this tool
|
11
|
+
# Do not remove this line
|
12
|
+
Cog::Config.instance.tool_generator_template = 'gorm/generator.rb'
|
13
|
+
|
14
|
+
# Custom cog tool gorm
|
3
15
|
module Gorm
|
4
16
|
|
5
17
|
# Creates a Schema under the given package +path+ and makes it available to a
|
6
18
|
# block that can then define it using the Schema::SchemaDefinitionMethods.
|
19
|
+
#
|
7
20
|
# ==== Arguments
|
8
21
|
# * +path+ - A / separated path where the source files should be generated.
|
9
22
|
# Do not include any file extension, they will be added automatically
|
10
23
|
# depending on the target language.
|
24
|
+
#
|
11
25
|
# ==== Options
|
12
26
|
# * <tt>:namespace</tt> - If provided, all generated classes will be placed
|
13
27
|
# inside this namespace.
|
14
28
|
def self.schema(path, opt={}, &block) # :yields: s
|
15
29
|
s = Schema.new path, opt
|
16
30
|
block.call s unless s.nil?
|
17
|
-
s.generate
|
31
|
+
s.generate
|
18
32
|
nil
|
19
33
|
end
|
20
34
|
|
21
|
-
def self.template_path(subpath) # :nodoc:
|
22
|
-
spec = Gem.loaded_specs['gorm']
|
23
|
-
prefix = if spec.nil?
|
24
|
-
File.expand_path File.join(File.dirname(__FILE__), '..')
|
25
|
-
else
|
26
|
-
spec.gem_dir
|
27
|
-
end
|
28
|
-
File.join prefix, 'templates', subpath
|
29
|
-
end
|
30
35
|
end
|
data/lib/gorm/schema.rb
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
require 'gorm/migration'
|
2
2
|
require 'gorm/table'
|
3
|
-
|
4
|
-
require 'cog/mixins/uses_templates'
|
3
|
+
require 'cog'
|
5
4
|
|
6
5
|
module Gorm
|
7
6
|
|
8
7
|
# A collection of migrations which cumulatively defines a database schema.
|
9
8
|
class Schema
|
10
9
|
|
11
|
-
include Cog::
|
10
|
+
include Cog::Generator
|
12
11
|
|
13
12
|
# Where to place the generated code.
|
14
13
|
attr_reader :output_path
|
@@ -40,11 +39,9 @@ module Gorm
|
|
40
39
|
end
|
41
40
|
|
42
41
|
# Generate the ORM source code
|
43
|
-
def generate
|
42
|
+
def generate
|
44
43
|
[:cpp, :h].each do |ext|
|
45
|
-
stamp
|
46
|
-
:use_absolute_path => true,
|
47
|
-
:target => "#{@output_path}.#{ext}"
|
44
|
+
stamp "gorm/schema.#{ext}", "#{@output_path}.#{ext}"
|
48
45
|
end
|
49
46
|
@tables.values.each {|t| t.generate! }
|
50
47
|
end
|
data/lib/gorm/table.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
require 'gorm/column'
|
2
2
|
require 'active_support/core_ext'
|
3
|
-
require 'cog
|
3
|
+
require 'cog'
|
4
4
|
|
5
5
|
module Gorm
|
6
6
|
|
7
7
|
# Represents a table in a Schema.
|
8
8
|
class Table
|
9
9
|
|
10
|
-
include Cog::
|
10
|
+
include Cog::Generator
|
11
11
|
|
12
12
|
# The name of the table in the database
|
13
13
|
attr_reader :table_name
|
@@ -31,9 +31,7 @@ module Gorm
|
|
31
31
|
# Generate the ORM source code
|
32
32
|
def generate!
|
33
33
|
[:cpp, :h].each do |ext|
|
34
|
-
stamp
|
35
|
-
:use_absolute_path => true,
|
36
|
-
:target => "#{@output_path}.#{ext}"
|
34
|
+
stamp "gorm/table.#{ext}", "#{@output_path}.#{ext}"
|
37
35
|
end
|
38
36
|
end
|
39
37
|
|
data/lib/gorm/version.rb
ADDED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gorm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kevin Tonon
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-11-05 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rake
|
@@ -77,26 +77,20 @@ dependencies:
|
|
77
77
|
version_requirements: *id004
|
78
78
|
description:
|
79
79
|
email: kevin@betweenconcepts.com
|
80
|
-
executables:
|
81
|
-
|
80
|
+
executables: []
|
81
|
+
|
82
82
|
extensions: []
|
83
83
|
|
84
84
|
extra_rdoc_files: []
|
85
85
|
|
86
86
|
files:
|
87
|
-
- bin/gorm
|
88
87
|
- LICENSE
|
89
|
-
- templates/example_database.rb
|
90
|
-
- templates/schema.cpp.erb
|
91
|
-
- templates/schema.h.erb
|
92
|
-
- templates/table.cpp.erb
|
93
|
-
- templates/table.h.erb
|
94
88
|
- lib/gorm/column.rb
|
95
89
|
- lib/gorm/migration.rb
|
96
90
|
- lib/gorm/schema.rb
|
97
91
|
- lib/gorm/table.rb
|
92
|
+
- lib/gorm/version.rb
|
98
93
|
- lib/gorm.rb
|
99
|
-
- lib/gorm_version.rb
|
100
94
|
homepage: https://github.com/ktonon/gorm
|
101
95
|
licenses: []
|
102
96
|
|
data/bin/gorm
DELETED
@@ -1,72 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
require 'gli'
|
3
|
-
require 'cog'
|
4
|
-
require 'fileutils'
|
5
|
-
begin # XXX: Remove this begin/rescue before distributing your app
|
6
|
-
require 'gorm'
|
7
|
-
rescue LoadError
|
8
|
-
STDERR.puts "In development, you need to use `bundle exec bin/todo` to run your app"
|
9
|
-
STDERR.puts "At install-time, RubyGems will make sure lib, etc. are in the load path"
|
10
|
-
STDERR.puts "Feel free to remove this message from bin/gorm now"
|
11
|
-
exit 64
|
12
|
-
end
|
13
|
-
|
14
|
-
include GLI::App
|
15
|
-
|
16
|
-
program_desc 'A code-Generating Object Relational Mapper for use with Qt and SQLite'
|
17
|
-
|
18
|
-
version Gorm::VERSION
|
19
|
-
|
20
|
-
desc 'Initialize gorm with a project'
|
21
|
-
command :init do |c|
|
22
|
-
c.action do |global_options,options,args|
|
23
|
-
include Cog::Mixins::UsesTemplates
|
24
|
-
unless File.exists? 'Cogfile'
|
25
|
-
FileUtils.cp File.join(Cog::Config.gem_dir, 'Default.cogfile'), 'Cogfile'
|
26
|
-
puts 'Created ./Cogfile'
|
27
|
-
end
|
28
|
-
config = Cog::Config.for_project
|
29
|
-
unless File.exists? config.generator_dir
|
30
|
-
FileUtils.mkdir_p config.generator_dir
|
31
|
-
puts "Created #{config.generator_dir}"
|
32
|
-
end
|
33
|
-
db_source = File.join(config.generator_dir, 'database.rb')
|
34
|
-
unless File.exists? db_source
|
35
|
-
FileUtils.cp Gorm.template_path('example_database.rb'), db_source
|
36
|
-
puts "Created #{db_source}"
|
37
|
-
end
|
38
|
-
puts "You may want to edit the Cogfile and change app_dir..."
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
desc 'Generate source files from the database.rb file'
|
43
|
-
command :generate do |c|
|
44
|
-
c.action do |global_options,options,args|
|
45
|
-
config = Cog::Config.for_project
|
46
|
-
db_source = File.join(config.generator_dir, 'database')
|
47
|
-
require db_source
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
pre do |global,command,options,args|
|
52
|
-
# Pre logic here
|
53
|
-
# Return true to proceed; false to abourt and not call the
|
54
|
-
# chosen command
|
55
|
-
# Use skips_pre before a command to skip this block
|
56
|
-
# on that command only
|
57
|
-
true
|
58
|
-
end
|
59
|
-
|
60
|
-
post do |global,command,options,args|
|
61
|
-
# Post logic here
|
62
|
-
# Use skips_post before a command to skip this
|
63
|
-
# block on that command only
|
64
|
-
end
|
65
|
-
|
66
|
-
on_error do |exception|
|
67
|
-
# Error logic here
|
68
|
-
# return false to skip default error handling
|
69
|
-
true
|
70
|
-
end
|
71
|
-
|
72
|
-
exit run(ARGV)
|
data/lib/gorm_version.rb
DELETED
data/templates/schema.cpp.erb
DELETED
@@ -1,124 +0,0 @@
|
|
1
|
-
<%= generated_warning %>
|
2
|
-
|
3
|
-
#include "<%= output_path %>.h"
|
4
|
-
#include "Bump/Bump.h"
|
5
|
-
#include <QFile>
|
6
|
-
#include <QtSql>
|
7
|
-
|
8
|
-
using namespace <%= namespace %>;
|
9
|
-
|
10
|
-
const QString DATABASE_FILENAME = "<%= namespace %>.sqlite";
|
11
|
-
|
12
|
-
<%= class_name %>::<%= class_name %>(QObject *parent) :
|
13
|
-
QObject(parent)
|
14
|
-
{
|
15
|
-
}
|
16
|
-
|
17
|
-
<%= class_name %>::~<%= class_name %>()
|
18
|
-
{
|
19
|
-
}
|
20
|
-
|
21
|
-
bool <%= class_name %>::open()
|
22
|
-
{
|
23
|
-
_qDatabase = QSqlDatabase::addDatabase("QSQLITE");
|
24
|
-
_qDatabase.setDatabaseName(sandboxPath(DATABASE_FILENAME));
|
25
|
-
return _qDatabase.open() && updateSchema();
|
26
|
-
}
|
27
|
-
|
28
|
-
bool <%= class_name %>::close()
|
29
|
-
{
|
30
|
-
_qDatabase.close();
|
31
|
-
return true;
|
32
|
-
}
|
33
|
-
|
34
|
-
bool <%= class_name %>::erase()
|
35
|
-
{
|
36
|
-
_qDatabase.close();
|
37
|
-
return QFile::remove(sandboxPath(DATABASE_FILENAME));
|
38
|
-
}
|
39
|
-
|
40
|
-
bool <%= class_name %>::updateSchema()
|
41
|
-
{
|
42
|
-
int nextVersion;
|
43
|
-
int version = currentSchemaVersion();
|
44
|
-
while (version < APP_SCHEMA_VERSION)
|
45
|
-
{
|
46
|
-
if (!applyIncrementalMigration(version + 1))
|
47
|
-
{
|
48
|
-
qCritical() << "Could not apply incremental migration" << QString::number(version + 1)
|
49
|
-
<< _qDatabase.lastError();
|
50
|
-
return false;
|
51
|
-
}
|
52
|
-
nextVersion = currentSchemaVersion();
|
53
|
-
if (nextVersion <= version)
|
54
|
-
{
|
55
|
-
qCritical() << "Incremental migration failed to increment the database schema number";
|
56
|
-
return false;
|
57
|
-
}
|
58
|
-
version = nextVersion;
|
59
|
-
}
|
60
|
-
return true;
|
61
|
-
}
|
62
|
-
|
63
|
-
int <%= class_name %>::currentSchemaVersion() const
|
64
|
-
{
|
65
|
-
Q_ASSERT(_qDatabase.isOpen() && _qDatabase.isValid());
|
66
|
-
if (_qDatabase.tables().count() == 0) return -1;
|
67
|
-
QSqlQuery query;
|
68
|
-
if (!query.exec("SELECT schema_version FROM meta WHERE id = 1") || !query.next())
|
69
|
-
{
|
70
|
-
qCritical() << "Unable to determine the current database schema version. The database is corrupt.";
|
71
|
-
abort();
|
72
|
-
}
|
73
|
-
return query.value(0).toInt();
|
74
|
-
}
|
75
|
-
|
76
|
-
bool <%= class_name %>::applyIncrementalMigration(int migrationVersion)
|
77
|
-
{
|
78
|
-
switch(migrationVersion)
|
79
|
-
{
|
80
|
-
<% migrations.each do |m| %>
|
81
|
-
case <%= m.index %>: return <%= m.method_call %>;
|
82
|
-
<% end %>
|
83
|
-
default:
|
84
|
-
qCritical() << "No such schema migration version" << QString::number(migrationVersion);
|
85
|
-
return false;
|
86
|
-
}
|
87
|
-
}
|
88
|
-
|
89
|
-
<% migrations.each do |m| %>
|
90
|
-
<%= m.method_stub :qualify => true %>
|
91
|
-
{
|
92
|
-
QSqlQuery query;
|
93
|
-
_qDatabase.transaction();
|
94
|
-
// TODO: migration body
|
95
|
-
query.exec("UPDATE meta SET schema_version = <%= m.index %> WHERE id = 1");
|
96
|
-
return _qDatabase.commit();
|
97
|
-
}
|
98
|
-
<% end %>
|
99
|
-
|
100
|
-
// bool <%= class_name %>::applyMigration0()
|
101
|
-
// {
|
102
|
-
// QSqlQuery query;
|
103
|
-
// _qDatabase.transaction();
|
104
|
-
// query.exec("CREATE TABLE meta (id INTEGER PRIMARY KEY, schema_version INTEGER)");
|
105
|
-
// query.exec("INSERT INTO meta VALUES (1, 1);");
|
106
|
-
// return _qDatabase.commit();
|
107
|
-
// }
|
108
|
-
//
|
109
|
-
// bool <%= class_name %>::applyMigration1()
|
110
|
-
// {
|
111
|
-
// QSqlQuery query;
|
112
|
-
// _qDatabase.transaction();
|
113
|
-
// query.exec("CREATE TABLE file_types (id INTEGER PRIMARY KEY, name VARCHAR(256))");
|
114
|
-
// query.exec("CREATE TABLE file_type_recognizers (id INTEGER PRIMARY KEY, file_type_id INTEGER, extension VARCHAR(16), path_pattern TEXT)");
|
115
|
-
// query.exec("CREATE TABLE matchers (id INTEGER PRIMARY KEY, file_type_id INTEGER, pattern TEXT)");
|
116
|
-
// query.exec("INSERT INTO file_types VALUES (NULL, 'Bump Log')");
|
117
|
-
// int fileTypeId = query.lastInsertId().toInt();
|
118
|
-
// query.prepare("INSERT INTO matchers VALUES (NULL, ?, ?)");
|
119
|
-
// query.bindValue(0, fileTypeId);
|
120
|
-
// query.bindValue(1, "(?P<year>\\d{4})-(?P<month>\\d{2})-(?P<day>\\d{2})");
|
121
|
-
// query.exec();
|
122
|
-
// query.exec("UPDATE meta SET schema_version = 2 WHERE id = 1");
|
123
|
-
// return _qDatabase.commit();
|
124
|
-
// }
|
data/templates/schema.h.erb
DELETED
@@ -1,76 +0,0 @@
|
|
1
|
-
<%= generated_warning %>
|
2
|
-
|
3
|
-
<%= include_guard_begin include_guard_name %>
|
4
|
-
|
5
|
-
#include <QObject>
|
6
|
-
#include <QString>
|
7
|
-
#include <QSqlDatabase>
|
8
|
-
|
9
|
-
<%= namespace_begin namespace %>
|
10
|
-
|
11
|
-
|
12
|
-
class <%= class_name %> : public QObject
|
13
|
-
{
|
14
|
-
Q_OBJECT
|
15
|
-
QSqlDatabase _qDatabase;
|
16
|
-
|
17
|
-
/** The schema version required by this release of the app. */
|
18
|
-
static const int APP_SCHEMA_VERSION = <%= app_schema_version %>;
|
19
|
-
|
20
|
-
public:
|
21
|
-
|
22
|
-
explicit <%= class_name %>(QObject *parent = 0);
|
23
|
-
virtual ~<%= class_name %>();
|
24
|
-
|
25
|
-
/** Open the database. */
|
26
|
-
bool open();
|
27
|
-
|
28
|
-
/** Close the database. */
|
29
|
-
bool close();
|
30
|
-
|
31
|
-
/** Erase the database. */
|
32
|
-
bool erase();
|
33
|
-
|
34
|
-
private:
|
35
|
-
|
36
|
-
/**
|
37
|
-
Apply the given incremental migration.
|
38
|
-
@param migrationVersion An integer indicating which migration to apply.
|
39
|
-
A migration version of 1 applies the migration which takes the database
|
40
|
-
from schema version 0 to 1. A migration version of 2 takes it from 1 to
|
41
|
-
2, and so on.
|
42
|
-
@returns @c false if the migration could not be applied.
|
43
|
-
*/
|
44
|
-
bool applyIncrementalMigration(int migrationVersion);
|
45
|
-
|
46
|
-
/**
|
47
|
-
The schema version of the current database.
|
48
|
-
Schema versions start at 0 and increase at integer intervals. Schema
|
49
|
-
version -1 has a special meaning: the database is empty. Schema version
|
50
|
-
0 installs a meta table for keeping track of the current database schema.
|
51
|
-
Schema version 1 is the schema used by the first release of the app,
|
52
|
-
version 2 by the second release, and so on.
|
53
|
-
@returns The schema version.
|
54
|
-
*/
|
55
|
-
int currentSchemaVersion() const;
|
56
|
-
|
57
|
-
/**
|
58
|
-
Update the database schema if necessary.
|
59
|
-
@returns @c false if the update failed.
|
60
|
-
*/
|
61
|
-
bool updateSchema();
|
62
|
-
|
63
|
-
private: // Schema Migrations
|
64
|
-
<% migrations.each do |m| %>
|
65
|
-
<%= m.method_stub %>;
|
66
|
-
<% end %>
|
67
|
-
|
68
|
-
signals:
|
69
|
-
|
70
|
-
public slots:
|
71
|
-
|
72
|
-
};
|
73
|
-
|
74
|
-
|
75
|
-
<%= namespace_end namespace %>
|
76
|
-
<%= include_guard_end %>
|
data/templates/table.cpp.erb
DELETED
File without changes
|
data/templates/table.h.erb
DELETED
File without changes
|