File tree 5 files changed +103
-40
lines changed
5 files changed +103
-40
lines changed Original file line number Diff line number Diff line change 18
18
19
19
use super :: tables:: table:: Table ;
20
20
21
+ #[ derive( Debug ) ]
21
22
pub struct Database {
22
- name : String ,
23
- tables : Vec < Table > ,
23
+ pub name : String ,
24
+ pub tables : Vec < Table > ,
24
25
}
25
26
26
27
impl Database {
27
- pub fn create_database ( name : String , tables : Vec < Table > ) -> Self {
28
+ pub fn create_database ( name : String ) -> Self {
28
29
Self {
29
30
name,
30
- tables,
31
+ tables : Vec :: new ( ) ,
31
32
}
32
33
}
33
34
34
- pub fn create_database_without_tables ( name : String ) -> Self {
35
- Self {
36
- name,
37
- tables : Vec :: new ( ) ,
38
- }
35
+ pub fn add_table ( & mut self , name : String ) {
36
+ self . tables . push ( Table :: create_table ( name) ) ;
39
37
}
40
38
41
39
pub fn delete_table ( & mut self , name : String ) {
42
- todo ! ( )
40
+ let mut ind = 0 ;
41
+ while ind != self . tables . len ( ) {
42
+ if self . tables [ ind] . name == name {
43
+ self . tables . remove ( ind) ;
44
+ } else {
45
+ ind += 1 ;
46
+ }
47
+ }
43
48
}
44
49
}
Original file line number Diff line number Diff line change 16
16
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17
17
*/
18
18
19
+ #[ derive( Debug ) ]
19
20
pub struct Row {
20
- primary_key : u32 ,
21
- columns : Vec < String > ,
21
+ pub primary_key : u32 ,
22
+ pub columns : Vec < String > ,
22
23
}
23
24
24
25
impl Row {
25
- pub fn create_row ( primary_key : u32 , columns : Vec < String > ) -> Self {
26
- Self {
27
- primary_key,
28
- columns,
29
- }
30
- }
31
-
32
- pub fn create_row_without_columns ( primary_key : u32 ) -> Self {
26
+ pub fn create_row ( primary_key : u32 ) -> Self {
33
27
Self {
34
28
primary_key,
35
29
columns : Vec :: new ( ) ,
36
30
}
37
31
}
38
32
39
- pub fn create_column ( & mut self , primary_key : u32 , name : String ) {
40
- todo ! ( )
33
+ pub fn create_column ( & mut self , name : String ) {
34
+ self . columns . push ( name ) ;
41
35
}
42
36
43
37
pub fn delete_column ( & mut self , name : String ) {
44
- todo ! ( )
38
+ let mut ind = 0 ;
39
+ while ind != self . columns . len ( ) {
40
+ if self . columns [ ind] == name {
41
+ self . columns . remove ( ind) ;
42
+ } else {
43
+ ind += 1 ;
44
+ }
45
+ }
45
46
}
46
47
}
Original file line number Diff line number Diff line change 16
16
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17
17
*/
18
18
19
- use std:: collections:: HashMap ;
20
19
use crate :: databases:: tables:: rows:: row:: Row ;
21
20
21
+ #[ derive( Debug ) ]
22
22
pub struct Table {
23
- name : String ,
24
- rows : HashMap < u32 , Row > ,
23
+ pub name : String ,
24
+ pub rows : Vec < Row > ,
25
25
}
26
26
27
27
impl Table {
28
- pub fn create_table ( name : String , rows : HashMap < u32 , Row > ) -> Self {
28
+ pub fn create_table ( name : String ) -> Self {
29
29
Self {
30
30
name,
31
- rows,
31
+ rows : Vec :: new ( ) ,
32
32
}
33
33
}
34
34
35
- pub fn create_table_without_rows ( name : String ) -> Self {
36
- Self {
37
- name,
38
- rows : HashMap :: new ( ) ,
39
- }
35
+ pub fn add_row ( & mut self , primary_key : u32 ) {
36
+ self . rows . push ( Row :: create_row ( primary_key) ) ;
40
37
}
41
38
42
39
pub fn delete_row ( & mut self , primary_key : u32 ) {
43
- todo ! ( )
40
+ let mut ind = 0 ;
41
+ while ind != self . rows . len ( ) {
42
+ if self . rows [ ind] . primary_key == primary_key {
43
+ self . rows . remove ( ind) ;
44
+ } else {
45
+ ind += 1 ;
46
+ }
47
+ }
44
48
}
45
49
}
Original file line number Diff line number Diff line change 19
19
pub ( crate ) mod databases;
20
20
use crate :: databases:: database:: Database ;
21
21
22
- struct RNSQL {
23
- databases : Vec < Database > ,
22
+ #[ derive( Debug ) ]
23
+ pub struct RNSQL {
24
+ pub databases : Vec < Database > ,
24
25
}
25
26
26
27
impl RNSQL {
27
- pub fn new ( ) {
28
- todo ! ( )
28
+ pub fn new ( ) -> Self {
29
+ Self {
30
+ databases : Vec :: new ( ) ,
31
+ }
32
+ }
33
+
34
+ pub fn add_database ( & mut self , name : String ) {
35
+ self . databases . push ( Database :: create_database ( name) ) ;
36
+ }
37
+
38
+ pub fn delete_database ( & mut self , name : String ) {
39
+ let mut ind = 0 ;
40
+ while ind != self . databases . len ( ) {
41
+ if self . databases [ ind] . name == name {
42
+ self . databases . remove ( ind) ;
43
+ } else {
44
+ ind += 1 ;
45
+ }
46
+ }
29
47
}
30
48
}
Original file line number Diff line number Diff line change 15
15
* You should have received a copy of the GNU General Public License
16
16
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17
17
*/
18
-
18
+ use rapid_naive_sql:: RNSQL ;
19
+
19
20
fn main ( ) {
20
- println ! ( "database" ) ;
21
+ let mut project = RNSQL :: new ( ) ;
22
+
23
+ println ! ( "{:#?}\n \n " , project) ;
24
+
25
+ project. add_database ( String :: from ( "db1" ) ) ;
26
+
27
+ println ! ( "{:#?}\n \n " , project) ;
28
+
29
+ project. databases [ 0 ] . add_table ( String :: from ( "tb1" ) ) ;
30
+
31
+ println ! ( "{:#?}\n \n " , project) ;
32
+
33
+ project. databases [ 0 ] . tables [ 0 ] . add_row ( 1 ) ;
34
+
35
+ println ! ( "{:#?}\n \n " , project) ;
36
+
37
+ project. databases [ 0 ] . tables [ 0 ] . rows [ 0 ] . create_column ( String :: from ( "name" ) ) ;
38
+
39
+ println ! ( "{:#?}\n \n " , project) ;
40
+
41
+ project. databases [ 0 ] . tables [ 0 ] . rows [ 0 ] . delete_column ( String :: from ( "name" ) ) ;
42
+
43
+ println ! ( "{:#?}\n \n " , project) ;
44
+
45
+ project. databases [ 0 ] . tables [ 0 ] . delete_row ( 1 ) ;
46
+
47
+ println ! ( "{:#?}\n \n " , project) ;
48
+
49
+ project. databases [ 0 ] . delete_table ( String :: from ( "tb1" ) ) ;
50
+
51
+ println ! ( "{:#?}\n \n " , project) ;
52
+
53
+ project. delete_database ( String :: from ( "db1" ) ) ;
54
+
55
+ println ! ( "{:#?}" , project) ;
21
56
}
You can’t perform that action at this time.
0 commit comments