/**gets some data from the database; $table is the name of the table, $cols is the list of columns to return or "*" for all, $where is the where clause of the SQL-statement; returns array of rows, which are in *_fetch_array format; returns false on error*/
public abstract function select($table,$cols,$where);
- /**insert values into a table*/
+ /**insert values into a table; returns false on failure, the new primary key if a sequence was set, true otherwise*/
public abstract function insert($table,array $values);
/**update database values*/
protected function tableName($tname){return $tname;}
/**returns the correct type name for the required abstract data type;
- types that must be understood are: int32 (INTEGER), int64 (LONG INTEGER), autoint32 (auto-incrementing int), autoint64, string:$length (text up to 255 chars, length is optional, default is 255; VARCHAR($length)), text (unlimited text)*/
+ types that must be understood are: int32 (INTEGER), int64 (LONG INTEGER), seq32 (auto-incrementing int), seq64, string:$length (text up to 255 chars, length is optional, default is 255; VARCHAR($length)), text (unlimited text)*/
protected function dataType($type)
{
if($type=="int32")return "INTEGER";
if($tpa[0]=="foreignkey"){
if(count($tpa)<3)
return false;
- return "REFERENCES $tpa[1]($tpa[2])";
+ return "REFERENCES ".$this->tableName($tpa[1])."($tpa[2])";
}
if($tpa[0]=="defaultint"){
if(count($tpa)<2)
$tabs=$dbScheme->tableNames();
for($i=0;$i<count($tabs);$i++){
if(!$this->createTable($tabs[$i],$dbScheme->tableDefinition($tabs[$i]))){
- print("DB Error: ".$this->lastError());
+ print("DB Error while creating ".$tabs[$i].": ".$this->lastError());
$this->rollbackTransaction();
die("Unable to create database.");
}
{
if($type=="int32")return "INT";
if($type=="int64")return "BIGINT";
- if($type=="autoint32")return "INT AUTO_INCREMENT";
- if($type=="autoint64")return "BIGINT AUTO_INCREMENT";
+ if($type=="seq32")return "INT AUTO_INCREMENT";
+ if($type=="seq64")return "BIGINT AUTO_INCREMENT";
if($type=="text")return "TEXT";
$tpa=explode(":",$type);
if($tpa[0]=="string"){
{
//FIXME: currently MySQL does not mark columns for indexing, since the syntax is somewhat different --> this needs to be fixed!
if($flag=="index")return "";
+ //fallback to SQL standard
+ return parent::columnFlag($flag);
}
public function insert($table,array $values)
{
- return mysql_query($this->sqlInsert($table,$values));
+ $res=mysql_query($this->sqlInsert($table,$values));
+ if($res===false)return false;
+ global $dbScheme;
+ $seq=$dbScheme->hasSequence($table);
+ if($seq!==false){
+ if(isset($values[$seq]))return $values[$seq];
+ $res=mysql_query("select LAST_INSERT_ID()");
+ if(mysql_num_rows($res)>0){
+ $row=mysql_fetch_array($res);
+ $ret=$row[0];
+ }else{
+ $ret=true;
+ }
+ mysql_free_result($res);
+ return $ret;
+ }else{
+ return true;
+ }
}
public function update($table,array $values,$where)
);
//event
$this->scheme["event"]=array(
- "eventid" => array("int32","primarykey"),
+ "eventid" => array("seq32","primarykey"),
//display data
"title" => array("string","notnull"),
"artist" => array("string","notnull"),
);
//customer
$this->scheme["customer"]=array(
- "customerid" => array("int32","primarykey"),
+ "customerid" => array("seq32","primarykey"),
//contact data
"name" => array("string",notnull),
"address" => array("string"),
);
//orders by customers
$this->scheme["order"]=array(
- "orderid" => array("int32","primarykey"),
+ "orderid" => array("seq32","primarykey"),
//customer
"customerid" => array("int32","foreignkey:customer:customerid"),
//seller (_online for web forms)
);
//tickets
$this->scheme["ticket"]=array(
- "ticketid" => array("int64","primarykey"),
+ "ticketid" => array("seq64","primarykey"),
"eventid" => array("int32","foreignkey:event:eventid"),
//initially a copy from event, can be adjusted by seller
"price" => array("int32","notnull"),
);
//buying tickets
$this->scheme["cart_ticket"]=array(
- "ctid" => array("int64","primarykey"),
+ "ctid" => array("seq64","primarykey"),
+ "cartid" => array("string:32","notnull","foreignkey:cart:cartid"),
//tickets in the cart
"eventid" => array("int32","foreignkey:event:eventid"),
"amount" => array("int32","notnull"),
);
//buying vouchers
$this->scheme["cart_voucher"]=array(
- "cvid" => array("int64","primarykey"),
+ "cvid" => array("seq64","primarykey"),
+ "cartid" => array("string:32","notnull","foreignkey:cart:cartid"),
//voucher value
"value" => array("int32","notnull")
);
return false;
$tpa=explode(":",$this->scheme[$tab][$col][0]);
switch($tpa[0]){
- case "int32":case "autoint32":case "int64":case "autoint64":
+ case "int32":case "seq32":case "int64":case "seq64":
return true;
default:
return false;
}
}
+ /**returns the sequence column name if the table has a sequence, false otherwise*/
+ public function hasSequence($tab)
+ {
+ if(!isset($this->scheme[$tab]))
+ return false;
+ foreach($this->scheme[$tab] as $cl => $def){
+ if($def[0] == "seq32" || $def[0] == "seq64")
+ return $cl;
+ }
+ return false;
+ }
+
/**returns true if the given column is of a string type*/
public function isStringColumn($tab,$col)
{
else
$data["cancelreason"]=false;
if($eventid=="new"){
- //create new ID
- $res=$db->select("event","max(eventid)","");
- if(count($res)==0)$eventid=0;
- else $eventid=$res[0][0]+1;
//create event
- $data["eventid"]=$eventid;
- $db->insert("event",$data);
+ $eventid=$db->insert("event",$data);
+ if($eventid===false){
+ header("X-MagicSmoke-Status: Error");
+ echo "Error accessing database.";
+ return;
+ }
}else{
//check ID
$eventid=$eventid+0;