1- #[ cfg( feature = "postgres" ) ] use crate :: constants:: postgresql_type;
2- #[ cfg( feature = "mssql" ) ] use crate :: constants:: sqlserver_type;
3- use crate :: constants:: { regex_patterns, rust_type} ;
4-
51pub mod handler;
62pub mod information_schema;
73pub mod memory;
84pub mod processor;
95pub mod transforms;
10-
11- use canyon_entities:: register_types:: CanyonRegisterEntityField ;
12- use regex:: Regex ;
13-
14- /// Return the postgres datatype and parameters to create a column for a given rust type
15- #[ cfg( feature = "postgres" ) ]
16- pub fn to_postgres_syntax ( field : & CanyonRegisterEntityField ) -> String {
17- let rust_type_clean = field. field_type . replace ( ' ' , "" ) ;
18-
19- match rust_type_clean. as_str ( ) {
20- rust_type:: I8 | rust_type:: U8 => {
21- String :: from ( & format ! ( "{} NOT NULL" , postgresql_type:: INTEGER ) )
22- }
23- rust_type:: OPT_I8 | rust_type:: OPT_U8 => String :: from ( postgresql_type:: INTEGER ) ,
24-
25- rust_type:: I16 | rust_type:: U16 => {
26- String :: from ( & format ! ( "{} NOT NULL" , postgresql_type:: INTEGER ) )
27- }
28- rust_type:: OPT_I16 | rust_type:: OPT_U16 => String :: from ( postgresql_type:: INTEGER ) ,
29-
30- rust_type:: I32 | rust_type:: U32 => {
31- String :: from ( & format ! ( "{} NOT NULL" , postgresql_type:: INTEGER ) )
32- }
33- rust_type:: OPT_I32 | rust_type:: OPT_U32 => String :: from ( postgresql_type:: INTEGER ) ,
34-
35- rust_type:: I64 | rust_type:: U64 => {
36- String :: from ( & format ! ( "{} NOT NULL" , postgresql_type:: BIGINT ) )
37- }
38- rust_type:: OPT_I64 | rust_type:: OPT_U64 => String :: from ( postgresql_type:: BIGINT ) ,
39-
40- rust_type:: STRING => String :: from ( & format ! ( "{} NOT NULL" , postgresql_type:: TEXT ) ) ,
41- rust_type:: OPT_STRING => String :: from ( postgresql_type:: TEXT ) ,
42-
43- rust_type:: BOOL => String :: from ( & format ! ( "{} NOT NULL" , postgresql_type:: BOOLEAN ) ) ,
44- rust_type:: OPT_BOOL => String :: from ( postgresql_type:: BOOLEAN ) ,
45-
46- rust_type:: NAIVE_DATE => String :: from ( & format ! ( "{} NOT NULL" , postgresql_type:: DATE ) ) ,
47- rust_type:: OPT_NAIVE_DATE => String :: from ( postgresql_type:: DATE ) ,
48-
49- rust_type:: NAIVE_TIME => String :: from ( & format ! ( "{} NOT NULL" , postgresql_type:: TIME ) ) ,
50- rust_type:: OPT_NAIVE_TIME => String :: from ( postgresql_type:: TIME ) ,
51-
52- rust_type:: NAIVE_DATE_TIME => {
53- String :: from ( & format ! ( "{} NOT NULL" , postgresql_type:: DATETIME ) )
54- }
55- rust_type:: OPT_NAIVE_DATE_TIME => String :: from ( postgresql_type:: DATETIME ) ,
56- & _ => todo ! ( "Not supported datatype for this migrations version" ) ,
57- }
58- }
59-
60- /// Return the postgres datatype and parameters to create a column for a given rust type
61- /// for Microsoft SQL Server
62- #[ cfg( feature = "mssql" ) ]
63- pub fn to_sqlserver_syntax ( field : & CanyonRegisterEntityField ) -> String {
64- let rust_type_clean = field. field_type . replace ( ' ' , "" ) ;
65-
66- match rust_type_clean. as_str ( ) {
67- rust_type:: I8 | rust_type:: U8 => {
68- String :: from ( & format ! ( "{} NOT NULL" , sqlserver_type:: INT ) )
69- }
70- rust_type:: OPT_I8 | rust_type:: OPT_U8 => String :: from ( sqlserver_type:: INT ) ,
71-
72- rust_type:: I16 | rust_type:: U16 => {
73- String :: from ( & format ! ( "{} NOT NULL" , sqlserver_type:: INT ) )
74- }
75- rust_type:: OPT_I16 | rust_type:: OPT_U16 => String :: from ( sqlserver_type:: INT ) ,
76-
77- rust_type:: I32 | rust_type:: U32 => {
78- String :: from ( & format ! ( "{} NOT NULL" , sqlserver_type:: INT ) )
79- }
80- rust_type:: OPT_I32 | rust_type:: OPT_U32 => String :: from ( sqlserver_type:: INT ) ,
81-
82- rust_type:: I64 | rust_type:: U64 => {
83- String :: from ( & format ! ( "{} NOT NULL" , sqlserver_type:: BIGINT ) )
84- }
85- rust_type:: OPT_I64 | rust_type:: OPT_U64 => String :: from ( sqlserver_type:: BIGINT ) ,
86-
87- rust_type:: STRING => {
88- String :: from ( & format ! ( "{} NOT NULL DEFAULT ''" , sqlserver_type:: NVARCHAR ) )
89- }
90- rust_type:: OPT_STRING => String :: from ( sqlserver_type:: NVARCHAR ) ,
91-
92- rust_type:: BOOL => String :: from ( & format ! ( "{} NOT NULL" , sqlserver_type:: BIT ) ) ,
93- rust_type:: OPT_BOOL => String :: from ( sqlserver_type:: BIT ) ,
94-
95- rust_type:: NAIVE_DATE => String :: from ( & format ! ( "{} NOT NULL" , sqlserver_type:: DATE ) ) ,
96- rust_type:: OPT_NAIVE_DATE => String :: from ( sqlserver_type:: DATE ) ,
97-
98- rust_type:: NAIVE_TIME => String :: from ( & format ! ( "{} NOT NULL" , sqlserver_type:: TIME ) ) ,
99- rust_type:: OPT_NAIVE_TIME => String :: from ( sqlserver_type:: TIME ) ,
100-
101- rust_type:: NAIVE_DATE_TIME => {
102- String :: from ( & format ! ( "{} NOT NULL" , sqlserver_type:: DATETIME ) )
103- }
104- rust_type:: OPT_NAIVE_DATE_TIME => String :: from ( sqlserver_type:: DATETIME ) ,
105- & _ => todo ! ( "Not supported datatype for this migrations version" ) ,
106- }
107- }
108-
109- #[ cfg( feature = "postgres" ) ]
110- pub fn to_postgres_alter_syntax ( field : & CanyonRegisterEntityField ) -> String {
111- let mut rust_type_clean = field. field_type . replace ( ' ' , "" ) ;
112- let rs_type_is_optional = field. field_type . to_uppercase ( ) . starts_with ( "OPTION" ) ;
113-
114- if rs_type_is_optional {
115- let type_regex = Regex :: new ( regex_patterns:: EXTRACT_RUST_OPT_REGEX ) . unwrap ( ) ;
116- let capture_rust_type = type_regex. captures ( rust_type_clean. as_str ( ) ) . unwrap ( ) ;
117- rust_type_clean = capture_rust_type
118- . name ( "rust_type" )
119- . unwrap ( )
120- . as_str ( )
121- . to_string ( ) ;
122- }
123-
124- match rust_type_clean. as_str ( ) {
125- rust_type:: I8 | rust_type:: U8 | rust_type:: OPT_I8 | rust_type:: OPT_U8 => {
126- String :: from ( postgresql_type:: INT_8 )
127- }
128- rust_type:: I16 | rust_type:: U16 | rust_type:: OPT_I16 | rust_type:: OPT_U16 => {
129- String :: from ( postgresql_type:: SMALL_INT )
130- }
131- rust_type:: I32 | rust_type:: U32 | rust_type:: OPT_I32 | rust_type:: OPT_U32 => {
132- String :: from ( postgresql_type:: INTEGER )
133- }
134- rust_type:: I64 | rust_type:: U64 | rust_type:: OPT_I64 | rust_type:: OPT_U64 => {
135- String :: from ( postgresql_type:: BIGINT )
136- }
137- rust_type:: STRING | rust_type:: OPT_STRING => String :: from ( postgresql_type:: TEXT ) ,
138- rust_type:: BOOL | rust_type:: OPT_BOOL => String :: from ( postgresql_type:: BOOLEAN ) ,
139- rust_type:: NAIVE_DATE | rust_type:: OPT_NAIVE_DATE => {
140- String :: from ( postgresql_type:: DATE )
141- }
142- rust_type:: NAIVE_TIME | rust_type:: OPT_NAIVE_TIME => {
143- String :: from ( postgresql_type:: TIME )
144- }
145- rust_type:: NAIVE_DATE_TIME | rust_type:: OPT_NAIVE_DATE_TIME => {
146- String :: from ( postgresql_type:: DATETIME )
147- }
148- & _ => todo ! ( "Not supported datatype for this migrations version" ) ,
149- }
150- }
151-
152- #[ cfg( feature = "mssql" ) ]
153- pub fn to_sqlserver_alter_syntax ( field : & CanyonRegisterEntityField ) -> String {
154- let mut rust_type_clean = field. field_type . replace ( ' ' , "" ) ;
155- let rs_type_is_optional = field. field_type . to_uppercase ( ) . starts_with ( "OPTION" ) ;
156-
157- if rs_type_is_optional {
158- let type_regex = Regex :: new ( regex_patterns:: EXTRACT_RUST_OPT_REGEX ) . unwrap ( ) ;
159- let capture_rust_type = type_regex. captures ( rust_type_clean. as_str ( ) ) . unwrap ( ) ;
160- rust_type_clean = capture_rust_type
161- . name ( "rust_type" )
162- . unwrap ( )
163- . as_str ( )
164- . to_string ( ) ;
165- }
166-
167- match rust_type_clean. as_str ( ) {
168- rust_type:: I8 | rust_type:: U8 | rust_type:: OPT_I8 | rust_type:: OPT_U8 => {
169- String :: from ( sqlserver_type:: TINY_INT )
170- }
171- rust_type:: I16 | rust_type:: U16 | rust_type:: OPT_I16 | rust_type:: OPT_U16 => {
172- String :: from ( sqlserver_type:: SMALL_INT )
173- }
174- rust_type:: I32 | rust_type:: U32 | rust_type:: OPT_I32 | rust_type:: OPT_U32 => {
175- String :: from ( sqlserver_type:: INT )
176- }
177- rust_type:: I64 | rust_type:: U64 | rust_type:: OPT_I64 | rust_type:: OPT_U64 => {
178- String :: from ( sqlserver_type:: BIGINT )
179- }
180- rust_type:: STRING | rust_type:: OPT_STRING => String :: from ( sqlserver_type:: NVARCHAR ) ,
181- rust_type:: BOOL | rust_type:: OPT_BOOL => String :: from ( sqlserver_type:: BIT ) ,
182- rust_type:: NAIVE_DATE | rust_type:: OPT_NAIVE_DATE => String :: from ( sqlserver_type:: DATE ) ,
183- rust_type:: NAIVE_TIME | rust_type:: OPT_NAIVE_TIME => String :: from ( sqlserver_type:: TIME ) ,
184- rust_type:: NAIVE_DATE_TIME | rust_type:: OPT_NAIVE_DATE_TIME => {
185- String :: from ( sqlserver_type:: DATETIME )
186- }
187- & _ => todo ! ( "Not supported datatype for this migrations version" ) ,
188- }
189- }
0 commit comments