Example #1
0
function InsertTestValues($database, $prepared_query, &$data)
{
    MetabaseQuerySetText($database, $prepared_query, 1, $data["user_name"]);
    MetabaseQuerySetText($database, $prepared_query, 2, $data["user_password"]);
    MetabaseQuerySetBoolean($database, $prepared_query, 3, $data["subscribed"]);
    MetabaseQuerySetInteger($database, $prepared_query, 4, $data["user_id"]);
    MetabaseQuerySetDecimal($database, $prepared_query, 5, $data["quota"]);
    MetabaseQuerySetFloat($database, $prepared_query, 6, $data["weight"]);
    MetabaseQuerySetDate($database, $prepared_query, 7, $data["access_date"]);
    MetabaseQuerySetTime($database, $prepared_query, 8, $data["access_time"]);
    MetabaseQuerySetTimestamp($database, $prepared_query, 9, $data["approved"]);
    return MetabaseExecuteQuery($database, $prepared_query);
}
 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;
 }