Esempio n. 1
0
 function CreateTable($table_name, $table)
 {
     MetabaseDebug($this->database, "Create table: " . $table_name);
     if (!MetabaseCreateTable($this->database, $table_name, $table["FIELDS"])) {
         return MetabaseError($this->database);
     }
     $success = 1;
     $error = "";
     if (isset($table["initialization"])) {
         $instructions = $table["initialization"];
         for (Reset($instructions), $instruction = 0; $success && $instruction < count($instructions); $instruction++, Next($instructions)) {
             switch ($instructions[$instruction]["type"]) {
                 case "insert":
                     $fields = $instructions[$instruction]["FIELDS"];
                     for ($query_fields = $query_values = "", Reset($fields), $field_number = 0; $field_number < count($fields); $field_number++, Next($fields)) {
                         if ($field_number > 0) {
                             $query_fields .= ",";
                             $query_values .= ",";
                         }
                         $field_name = Key($fields);
                         $field = $table["FIELDS"][$field_name];
                         if (strcmp($error = $this->GetField($field, $field_name, 0, $query), "")) {
                             return $error;
                         }
                         $query_fields .= $query;
                         $query_values .= "?";
                     }
                     if ($success = $prepared_query = MetabasePrepareQuery($this->database, "INSERT INTO {$table_name} ({$query_fields}) VALUES ({$query_values})")) {
                         for ($lobs = array(), Reset($fields), $field_number = 0; $field_number < count($fields); $field_number++, Next($fields)) {
                             $field_name = Key($fields);
                             $field = $table["FIELDS"][$field_name];
                             if (strcmp($error = $this->GetField($field, $field_name, 0, $query), "")) {
                                 return $error;
                             }
                             switch ($field["type"]) {
                                 case "integer":
                                     $success = MetabaseQuerySetInteger($this->database, $prepared_query, $field_number + 1, intval($fields[$field_name]));
                                     break;
                                 case "text":
                                     $success = MetabaseQuerySetText($this->database, $prepared_query, $field_number + 1, $fields[$field_name]);
                                     break;
                                 case "clob":
                                     $lob_definition = array("Database" => $this->database, "Error" => "", "Data" => $fields[$field_name]);
                                     $lob = count($lobs);
                                     if (!($success = MetabaseCreateLOB($lob_definition, $lobs[$lob]))) {
                                         $error = $lob_definition["Error"];
                                         break;
                                     }
                                     $success = MetabaseQuerySetCLOB($this->database, $prepared_query, $field_number + 1, $lobs[$lob], $field_name);
                                     break;
                                 case "blob":
                                     $lob_definition = array("Database" => $this->database, "Error" => "", "Data" => $fields[$field_name]);
                                     $lob = count($lobs);
                                     if (!($success = MetabaseCreateLOB($lob_definition, $lobs[$lob]))) {
                                         $error = $lob_definition["Error"];
                                         break;
                                     }
                                     $success = MetabaseQuerySetBLOB($this->database, $prepared_query, $field_number + 1, $lobs[$lob], $field_name);
                                     break;
                                 case "boolean":
                                     $success = MetabaseQuerySetBoolean($this->database, $prepared_query, $field_number + 1, intval($fields[$field_name]));
                                     break;
                                 case "date":
                                     $success = MetabaseQuerySetDate($this->database, $prepared_query, $field_number + 1, $fields[$field_name]);
                                     break;
                                 case "timestamp":
                                     $success = MetabaseQuerySetTimestamp($this->database, $prepared_query, $field_number + 1, $fields[$field_name]);
                                     break;
                                 case "time":
                                     $success = MetabaseQuerySetTime($this->database, $prepared_query, $field_number + 1, $fields[$field_name]);
                                     break;
                                 case "float":
                                     $success = MetabaseQuerySetFloat($this->database, $prepared_query, $field_number + 1, doubleval($fields[$field_name]));
                                     break;
                                 case "decimal":
                                     $success = MetabaseQuerySetDecimal($this->database, $prepared_query, $field_number + 1, $fields[$field_name]);
                                     break;
                                 default:
                                     $error = "type \"" . $field["type"] . "\" is not yet supported";
                                     $success = 0;
                                     break;
                             }
                             if (!$success && $error == "") {
                                 $error = MetabaseError($this->database);
                                 break;
                             }
                         }
                         if ($success && !($success = MetabaseExecuteQuery($this->database, $prepared_query))) {
                             $error = MetabaseError($this->database);
                         }
                         for ($lob = 0; $lob < count($lobs); $lob++) {
                             MetabaseDestroyLOB($lobs[$lob]);
                         }
                         MetabaseFreePreparedQuery($this->database, $prepared_query);
                     } else {
                         $error = MetabaseError($this->database);
                     }
                     break;
             }
         }
     }
     if ($success && isset($table["INDEXES"])) {
         if (!MetabaseSupport($this->database, "Indexes")) {
             return "indexes are not supported";
         }
         $indexes = $table["INDEXES"];
         for ($index = 0, Reset($indexes); $index < count($indexes); Next($indexes), $index++) {
             if (!MetabaseCreateIndex($this->database, $table_name, Key($indexes), $indexes[Key($indexes)])) {
                 $error = MetabaseError($this->database);
                 $success = 0;
                 break;
             }
         }
     }
     if (!$success) {
         if (!MetabaseDropTable($this->database, $table_name)) {
             $error = "could not initialize the table \"{$table_name}\" ({$error}) and then could not drop the table (" . MetabaseError($this->database) . ")";
         }
     }
     return $error;
 }
Esempio n. 2
0
                 $binary_data .= chr($code);
             }
             $binary_lob = array("Type" => "inputfile", "Database" => $database, "Error" => "", "FileName" => $binary_data_file, "BufferLength" => 32);
             $success = fwrite($file, $binary_data, strlen($binary_data)) == strlen($binary_data);
             fclose($file);
         } else {
             $success = 0;
         }
     }
 } else {
     $sucess = 0;
 }
 if ($success) {
     if ($success = MetabaseCreateLOB($character_lob, $clob)) {
         if ($success = MetabaseCreateLOB($binary_lob, $blob)) {
             MetabaseQuerySetCLOB($database, $prepared_query, 1, $clob, "document");
             MetabaseQuerySetBLOB($database, $prepared_query, 2, $blob, "picture");
             $success = MetabaseExecuteQuery($database, $prepared_query);
             MetabaseDestroyLOB($blob);
         } else {
             $error = $binary_lob["Error"];
         }
         MetabaseDestroyLOB($clob);
     } else {
         $error = $character_lob["Error"];
     }
     MetabaseFreePreparedQuery($database, $prepared_query);
     if (!$success || !($result = MetabaseQuery($database, "SELECT document,picture FROM files"))) {
         $success = 0;
     } else {
         if (MetabaseEndOfResult($database, $result)) {