File: Schema.h

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • 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 (137 lines) | stat: -rw-r--r-- 3,148 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
#pragma once
#include "Core/Array.h"
#include "Core/Str.h"
#include "Utils/Bitmask.h"
#include "Type.h"
#include "QueryStr.h"

namespace sql {

	/**
	 * The Schema class contains the structure from a table within your database.
	 * To get the Schema one would need to call the schema() function in a database class.
	 */
	class Schema : public Object {
		STORM_CLASS;
	public:
		/**
		 * Attributes for a column.
		 */
		enum Attributes {
			// No attributes.
			none,
			// Primary key.
			primaryKey = 0x01,
			// Not null.
			notNull = 0x02,
			// Unique.
			unique = 0x04,
			// Autoincrement.
			autoIncrement = 0x08,
		};

		/**
		 * Schema contains the content class, which holds the name, datatype and all attributes for
		 * a given row.  This row is specified in the getRow function from the Schema class.
		 */
		class Column : public Object {
			STORM_CLASS;
		public:
			// Constructors of the Column class.
			STORM_CTOR Column(Str *name, QueryType dt);
			STORM_CTOR Column(Str *name, QueryType dt, Attributes attributes);

			// Name of the column.
			Str *name;

			// Datatype of the column.
			QueryType type;

			// Attribtes.
			Attributes attributes;

			// Default value, if any.
			MAYBE(Str *) STORM_NAME(defaultValue, default);

			// Unknown parts of the declaration. If 'type' is VOID, then the type is here also.
			MAYBE(Str *) unknown;

			// To string.
			virtual void STORM_FN toS(StrBuf *to) const override;

			// To SQL.
			virtual void STORM_FN toSQL(QueryStrBuilder *to) const;
		};

		/**
		 * An index on a particular table.
		 */
		class Index : public Object {
			STORM_CLASS;
		public:
			// Create.
			STORM_CTOR Index(Str *name, Array<Str *> *columns);

			// Name of the index.
			Str *name;

			// Columns indexed.
			Array<Str *> *columns;

			// To string.
			virtual void STORM_FN toS(StrBuf *to) const override;

			// To SQL.
			void STORM_FN toSQL(QueryStrBuilder *to, Str *table) const;
		};

		// Create a filled schema.
		STORM_CTOR Schema(Str *tableName, Array<Column *> *columns);
		STORM_CTOR Schema(Str *tableName, Array<Column *> *columns, Array<Index *> *indices);

		// Number of columns in this table.
		Nat STORM_FN count() const {
			return columns->count();
		}

		// The name of this table.
		Str *STORM_FN name() const {
			return tableName;
		}

		// Get a column.
		Column *STORM_FN at(Nat id) const {
			return columns->at(id);
		}
		Column *STORM_FN operator[](Nat id) const {
			return columns->at(id);
		}

		// Indices.
		Array<Index *> *STORM_FN indices() const;

		// To string.
		virtual void STORM_FN toS(StrBuf *to) const override;

		// To SQL.
		virtual void STORM_FN toSQL(QueryStrBuilder *to) const;

	private:
		// Name of this table.
		Str *tableName;

		// Columns.
		Array<Column *> *columns;

		// Indices.
		Array<Index *> *index;
	};

	BITMASK_OPERATORS(Schema::Attributes);

	// Convert attributes into SQL-like names. Always starts with a space.
	void STORM_FN toSQL(StrBuf *to, Schema::Attributes attributes);
	Str *STORM_FN toSQL(EnginePtr e, Schema::Attributes attributes);
	void STORM_FN toSQL(QueryStrBuilder *to, Schema::Attributes attributes);

}