File: Migration.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (145 lines) | stat: -rw-r--r-- 3,678 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "stdafx.h"
#include "Migration.h"
#include "Core/Join.h"

namespace sql {

	Migration::Migration() {
		tableAdd = new (this) Array<Schema *>();
		tableMigrate = new (this) Array<Table *>();
		tableRemove = new (this) Array<Str *>();
		indexAdd = new (this) Array<Index *>();
		indexRemove = new (this) Array<Index *>();
	}

	Bool Migration::any() const {
		if (tableAdd->any() || tableRemove->any() || indexAdd->any() || indexRemove->any())
			return true;

		for (Nat i = 0; i < tableMigrate->count(); i++)
			if (tableMigrate->at(i)->any())
				return true;

		return false;
	}

	Bool Migration::empty() const {
		return !any();
	}

	void Migration::toS(StrBuf *to) const {
		*to << S("{");
		{
			storm::Indent z(to);
			if (tableRemove->any())
				*to << S("\nremove tables: ") << join(tableRemove, S(", "));
			if (tableMigrate->any()) {
				*to << S("\nmigrate tables:\n");
				storm::Indent w(to);
				*to << join(tableMigrate, S("\n"));
			}
			if (tableAdd->any()) {
				*to << S("\nadd tables:\n");
				storm::Indent w(to);
				*to << join(tableAdd, S("\n"));
			}
			if (indexRemove->any())
				*to << S("\nremove index: ") << join(indexRemove, S(", "));
			if (indexAdd->any()) {
				*to << S("\nadd index:\n");
				storm::Indent w(to);
				*to << join(indexAdd, S("\n"));
			}
		}
		*to << S("\n}");
	}

	Migration::ColAttrs::ColAttrs(Str *name, QueryType type) : name(name), type(type) {}

	Bool Migration::ColAttrs::any() const {
		if (currentAttributes != desiredAttributes)
			return true;

		if ((currentDefault != null) != (desiredDefault != null))
			return true;

		if (currentDefault && desiredDefault)
			return *currentDefault != *desiredDefault;

		return false;
	}

	Bool Migration::ColAttrs::empty() const {
		return !any();
	}

	void Migration::ColAttrs::toS(StrBuf *to) const {
		*to << name << S(" ") << type << S(" => current:");
		sql::toSQL(to, currentAttributes);
		if (currentDefault)
			*to << S(" DEFAULT ") << currentDefault;
		*to << S(", desired:");
		sql::toSQL(to, desiredAttributes);
		if (desiredDefault)
			*to << S(" DEFAULT ") << desiredDefault;
	}

	Schema::Column *Migration::ColAttrs::toSchema() const {
		Schema::Column *col = new (this) Schema::Column(name, type, desiredAttributes);
		col->defaultValue = desiredDefault;
		return col;
	}


	Migration::Table::Table(Str *table) : table(table) {
		colRemove = new (this) Array<Str *>();
		colMigrate = new (this) Array<ColAttrs *>();
		colAdd = new (this) Array<Schema::Column *>();
		updatePrimaryKeys = false;
		primaryKeys = new (this) Array<Str *>();
	}

	Bool Migration::Table::any() const {
		return colRemove->any()
			|| colMigrate->any()
			|| colAdd->any()
			|| updatePrimaryKeys;
	}

	Bool Migration::Table::empty() const {
		return !any();
	}

	void Migration::Table::toS(StrBuf *to) const {
		*to << table << S(" : {");
		{
			storm::Indent z(to);
			if (colRemove->any())
				*to << S("\nremove columns: ") << join(colRemove, S(", "));
			if (colMigrate->any()) {
				*to << S("\nmigrate columns:\n");
				storm::Indent w(to);
				*to << join(colMigrate, S("\n"));
			}
			if (colAdd->any())
				*to << S("\nadd columns: ") << join(colAdd, S(", "));
		}
		*to << S("\n}");
	}


	Migration::Index::Index(Str *table, Schema::Index *index)
		: Schema::Index(*index), table(table) {}

	Migration::Index::Index(Str *table, Str *name, Array<Str *> *columns)
		: Schema::Index(name, columns), table(table) {}

	void Migration::Index::toS(StrBuf *to) const {
		*to << S("INDEX ") << name << S(" ON ") << table << S("(") << join(columns, S(", ")) << S(")");
	}

	void Migration::Index::toSQL(QueryStrBuilder *to) const {
		Schema::Index::toSQL(to, table);
	}

}