gorm 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/gorm +72 -0
- data/lib/gorm/schema.rb +1 -0
- data/lib/gorm/table.rb +13 -0
- data/lib/gorm_version.rb +1 -1
- data/templates/example_database.rb +12 -0
- data/templates/schema.cpp.erb +124 -0
- data/templates/schema.h.erb +76 -0
- data/templates/table.cpp.erb +0 -0
- data/templates/table.h.erb +0 -0
- metadata +42 -6
data/bin/gorm
ADDED
@@ -0,0 +1,72 @@
|
|
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/schema.rb
CHANGED
data/lib/gorm/table.rb
CHANGED
@@ -1,11 +1,14 @@
|
|
1
1
|
require 'gorm/column'
|
2
2
|
require 'active_support/core_ext'
|
3
|
+
require 'cog/mixins/uses_templates'
|
3
4
|
|
4
5
|
module Gorm
|
5
6
|
|
6
7
|
# Represents a table in a Schema.
|
7
8
|
class Table
|
8
9
|
|
10
|
+
include Cog::Mixins::UsesTemplates
|
11
|
+
|
9
12
|
# The name of the table in the database
|
10
13
|
attr_reader :table_name
|
11
14
|
|
@@ -16,6 +19,7 @@ module Gorm
|
|
16
19
|
@schema = schema
|
17
20
|
@table_name = name.to_s.pluralize.dasherize
|
18
21
|
@class_name = name.to_s.singularize.camelize
|
22
|
+
@output_path = File.join schema.output_path, @class_name
|
19
23
|
@columns = {}
|
20
24
|
end
|
21
25
|
|
@@ -24,6 +28,15 @@ module Gorm
|
|
24
28
|
@columns.values.sort
|
25
29
|
end
|
26
30
|
|
31
|
+
# Generate the ORM source code
|
32
|
+
def generate!
|
33
|
+
[:cpp, :h].each do |ext|
|
34
|
+
stamp Gorm::template_path("table.#{ext}"),
|
35
|
+
:use_absolute_path => true,
|
36
|
+
:target => "#{@output_path}.#{ext}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
27
40
|
# An interface for defining a Table
|
28
41
|
module TableDefinitionMethods
|
29
42
|
# Define a Column in this table.
|
data/lib/gorm_version.rb
CHANGED
@@ -0,0 +1,124 @@
|
|
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
|
+
// }
|
@@ -0,0 +1,76 @@
|
|
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 %>
|
File without changes
|
File without changes
|
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: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kevin Tonon
|
@@ -45,16 +45,52 @@ dependencies:
|
|
45
45
|
version: "0"
|
46
46
|
type: :development
|
47
47
|
version_requirements: *id002
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: aruba
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
type: :development
|
61
|
+
version_requirements: *id003
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: gli
|
64
|
+
prerelease: false
|
65
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - "="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
hash: 29
|
71
|
+
segments:
|
72
|
+
- 2
|
73
|
+
- 4
|
74
|
+
- 1
|
75
|
+
version: 2.4.1
|
76
|
+
type: :runtime
|
77
|
+
version_requirements: *id004
|
48
78
|
description:
|
49
79
|
email: kevin@betweenconcepts.com
|
50
|
-
executables:
|
51
|
-
|
80
|
+
executables:
|
81
|
+
- gorm
|
52
82
|
extensions: []
|
53
83
|
|
54
84
|
extra_rdoc_files: []
|
55
85
|
|
56
86
|
files:
|
87
|
+
- bin/gorm
|
57
88
|
- 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
|
58
94
|
- lib/gorm/column.rb
|
59
95
|
- lib/gorm/migration.rb
|
60
96
|
- lib/gorm/schema.rb
|
@@ -96,6 +132,6 @@ rubyforge_project:
|
|
96
132
|
rubygems_version: 1.8.24
|
97
133
|
signing_key:
|
98
134
|
specification_version: 3
|
99
|
-
summary: A code-Generating Object Relational Mapper.
|
135
|
+
summary: A code-Generating Object Relational Mapper for use with Qt and SQLite.
|
100
136
|
test_files: []
|
101
137
|
|