@@ -454,8 +454,19 @@ public function createTable($name, array $prototype)
454454 }
455455 if ($ has_tp ) {
456456 if (preg_match ('#link\((.+)\)# ' , $ prop ['type ' ], $ link )) {
457- print_r ($ link );
458- exit ;
457+ $ link_info = explode ('. ' , $ link [1 ]);
458+ $ link_table_path = $ this ->_getTablePath ($ link_info [0 ]);
459+ if (!file_exists ($ link_table_path )) {
460+ throw new Exception ("JSONDB Error: Can't create the table \"{$ name }\". An error occur when linking the column \"{$ field }\" with the column \"{$ link [1 ]}\", the table \"{$ link_info [0 ]}\" doesn't exist in the database \"{$ this ->database }\". " );
461+ }
462+
463+ $ link_table_data = $ this ->getTableData ($ link_table_path );
464+ if (!in_array ($ link_info [1 ], $ link_table_data ['prototype ' ], TRUE )) {
465+ throw new Exception ("JSONDB Error: Can't create the table \"{$ name }\". An error occur when linking the column \"{$ field }\" with the column \"{$ link [1 ]}\", the column \"{$ link_info [1 ]}\" doesn't exist in the table \"{$ link_info [0 ]}\". " );
466+ }
467+ if ((array_key_exists ('primary_keys ' , $ link_table_data ['properties ' ]) && !in_array ($ link_info [1 ], $ link_table_data ['properties ' ]['primary_keys ' ], TRUE )) || (array_key_exists ('unique_keys ' , $ link_table_data ['properties ' ]) && !in_array ($ link_info [1 ], $ link_table_data ['properties ' ]['unique_keys ' ], TRUE ))) {
468+ throw new Exception ("JSONDB Error: Can't create the table \"{$ name }\". An error occur when linking the column \"{$ field }\" with the column \"{$ link [1 ]}\", the column \"{$ link_info [1 ]}\" is not a PRIMARY KEY or an UNIQUE KEY of the table \"{$ link_info [0 ]}\". " );
469+ }
459470 }
460471 } else {
461472 $ prototype [$ field ]['type ' ] = 'string ' ;
@@ -558,13 +569,13 @@ private function execute()
558569 }
559570
560571 /**
561- * Returns a data with his row id
572+ * Returns a table's data
562573 * @param string|null $path The path to the table
563574 * @return array
564575 */
565576 public function getTableData ($ path = NULL )
566577 {
567- return json_decode (file_get_contents ($ path ), TRUE );
578+ return json_decode (file_get_contents (NULL !== $ path ? $ path : $ this -> _getTablePath () ), TRUE );
568579 }
569580
570581 /**
@@ -650,43 +661,55 @@ protected function _parseValue($value, $properties)
650661 {
651662 if (NULL !== $ value || (array_key_exists ('not_null ' , $ properties ) && TRUE === $ properties ['not_null ' ])) {
652663 if (array_key_exists ('type ' , $ properties )) {
653- switch ($ properties ['type ' ]) {
654- case 'int ' :
655- case 'integer ' :
656- case 'number ' :
657- $ value = (int )$ value ;
658- break ;
659-
660- case 'decimal ' :
661- case 'float ' :
662- $ value = (float )$ value ;
663- if (array_key_exists ('max_length ' , $ properties )) {
664- $ value = number_format ($ value , $ properties ['max_length ' ]);
664+ if (preg_match ('#link\((.+)\)# ' , $ properties ['type ' ], $ link )) {
665+ $ link_info = explode ('. ' , $ link [1 ]);
666+ $ link_table_path = $ this ->_getTablePath ($ link_info [0 ]);
667+ $ link_table_data = $ this ->getTableData ($ link_table_path );
668+ $ value = $ this ->_parseValue ($ value , $ link_table_data ['properties ' ][$ link_info [1 ]]);
669+ foreach ((array )$ link_table_data ['data ' ] as $ data ) {
670+ if ($ data [$ link_info [1 ]] === $ value ) {
671+ return $ data ['#rowid ' ];
665672 }
666- break ;
667-
668- case 'string ' :
669- $ value = (string )$ value ;
670- if (array_key_exists ('max_length ' , $ properties ) && strlen ($ value ) > 0 ) {
671- $ value = substr ($ value , 0 , $ properties ['max_length ' ]);
672- }
673- break ;
674-
675- case 'char ' :
676- $ value = (string )$ value [0 ];
677- break ;
678-
679- case 'bool ' :
680- case 'boolean ' :
681- $ value = (bool )$ value ;
682- break ;
683-
684- case 'array ' :
685- $ value = (array )$ value ;
686- break ;
687-
688- default :
689- throw new Exception ("JSONDB Error: Trying to parse a value with an unsupported type \"{$ properties ['type ' ]}\"" );
673+ }
674+ } else {
675+ switch ($ properties ['type ' ]) {
676+ case 'int ' :
677+ case 'integer ' :
678+ case 'number ' :
679+ $ value = (int )$ value ;
680+ break ;
681+
682+ case 'decimal ' :
683+ case 'float ' :
684+ $ value = (float )$ value ;
685+ if (array_key_exists ('max_length ' , $ properties )) {
686+ $ value = number_format ($ value , $ properties ['max_length ' ]);
687+ }
688+ break ;
689+
690+ case 'string ' :
691+ $ value = (string )$ value ;
692+ if (array_key_exists ('max_length ' , $ properties ) && strlen ($ value ) > 0 ) {
693+ $ value = substr ($ value , 0 , $ properties ['max_length ' ]);
694+ }
695+ break ;
696+
697+ case 'char ' :
698+ $ value = (string )$ value [0 ];
699+ break ;
700+
701+ case 'bool ' :
702+ case 'boolean ' :
703+ $ value = (bool )$ value ;
704+ break ;
705+
706+ case 'array ' :
707+ $ value = (array )$ value ;
708+ break ;
709+
710+ default :
711+ throw new Exception ("JSONDB Error: Trying to parse a value with an unsupported type \"{$ properties ['type ' ]}\"" );
712+ }
690713 }
691714 }
692715 } elseif (array_key_exists ('default ' , $ properties )) {
0 commit comments