metabase/metabase_mysql.php - view - 1.30
1: <?
2: if(!defined("METABASE_MYSQL_INCLUDED"))
3: {
4: define("METABASE_MYSQL_INCLUDED",1);
5:
6: /*
7: * metabase_mysql.php
8: *
9: * @(#) $Header: /opt2/mlemos/cvs/metal/metabase/metabase_mysql.php,v 1.30 2000/02/15 03:02:13 mlemos Exp $
10: *
11: */
12:
13: class metabase_mysql_class extends metabase_database_class
14: {
15: var $connection=0;
16: var $connected_host;
17: var $connected_user;
18: var $connected_password;
19: var $connected_port;
20: var $opened_persistent="";
21: var $decimal_factor=1.0;
22:
23: Function Connect()
24: {
25: $port=(IsSet($this->options["Port"]) ? $this->options["Port"] : "");
26: if($this->connection!=0)
27: {
28: if(!strcmp($this->connected_host,$this->host)
29: && !strcmp($this->connected_user,$this->user)
30: && !strcmp($this->connected_password,$this->password)
31: && !strcmp($this->connected_port,$port)
32: && $this->opened_persistent==$this->persistent)
33: return(1);
34: mysql_Close($this->connection);
35: $this->connection=0;
36: $this->affected_rows=-1;
37: }
38: $function=($this->persistent ? "mysql_pconnect" : "mysql_connect");
39: if(($this->connection=@$function($this->host.(!strcmp($port,"") ? "" : ":".$port),$this->user,$this->password))<=0)
40: {
41: $this->last_error=(IsSet($php_errormsg) ? $php_errormsg : "Could not connect to MySQL server");
42: return(0);
43: }
44: $this->connected_host=$this->host;
45: $this->connected_user=$this->user;
46: $this->connected_password=$this->password;
47: $this->connected_port=$port;
48: $this->opened_persistent=$this->persistent;
49: return(1);
50: }
51:
52: Function Close()
53: {
54: if($this->connection!=0)
55: {
56: mysql_Close($this->connection);
57: $this->connection=0;
58: $this->affected_rows=-1;
59: }
60: }
61:
62: Function Query($query)
63: {
64: if(!strcmp($this->database_name,""))
65: {
66: $this->last_error="It was not specified a valid database name to select";
67: return(0);
68: }
69: if(!$this->Connect())
70: return(0);
71: if(($result=mysql_db_query($this->database_name,$query,$this->connection)))
72: $this->affected_rows=mysql_affected_rows($this->connection);
73: else
74: $this->last_error=mysql_error($this->connection);
75: return($result);
76: }
77:
78: Function FetchResult($result,$row,$field)
79: {
80: return(mysql_result($result,$row,$field));
81: }
82:
83: Function FetchDecimalResult($result,$row,$field)
84: {
85: return(sprintf("%.".$this->decimal_places."f",$this->FetchResult($result,$row,$field)/$this->decimal_factor));
86: }
87:
88: Function NumberOfRows($result)
89: {
90: return(mysql_num_rows($result));
91: }
92:
93: Function FreeResult($result)
94: {
95: return(mysql_free_result($result));
96: }
97:
98: Function Error()
99: {
100: return($this->last_error);
101: }
102:
103: Function CreateDatabase($name)
104: {
105: if(!$this->Connect())
106: return(0);
107: if(!mysql_create_db($name,$this->connection))
108: {
109: $this->last_error=mysql_error($this->connection);
110: return(0);
111: }
112: return(1);
113: }
114:
115: Function DropDatabase($name)
116: {
117: if(!$this->Connect())
118: return(0);
119: if(!mysql_drop_db($name,$this->connection))
120: {
121: $this->last_error=mysql_error($this->connection);
122: return(0);
123: }
124: return(1);
125: }
126:
127: Function GetTextFieldValue($value)
128: {
129: return("'".AddSlashes($value)."'");
130: }
131:
132: Function GetIntegerFieldTypeDeclaration($name,&$field)
133: {
134: return("$name ".(IsSet($field["unsigned"]) ? "INT UNSIGNED" : "INT").(IsSet($field["default"]) ? " DEFAULT ".$field["default"] : "").(IsSet($field["notnull"]) ? " NOT NULL" : ""));
135: }
136:
137: Function GetDateFieldTypeDeclaration($name,&$field)
138: {
139: return($name." DATE".(IsSet($field["default"]) ? " DEFAULT '".$field["default"]."'" : "").(IsSet($field["notnull"]) ? " NOT NULL" : ""));
140: }
141:
142: Function GetTimestampFieldTypeDeclaration($name,&$field)
143: {
144: return($name." DATETIME".(IsSet($field["default"]) ? " DEFAULT '".$field["default"]."'" : "").(IsSet($field["notnull"]) ? " NOT NULL" : ""));
145: }
146:
147: Function GetTimeFieldTypeDeclaration($name,&$field)
148: {
149: return($name." TIME".(IsSet($field["default"]) ? " DEFAULT '".$field["default"]."'" : "").(IsSet($field["notnull"]) ? " NOT NULL" : ""));
150: }
151:
152: Function GetFloatFieldTypeDeclaration($name,&$field)
153: {
154: return("$name DOUBLE ".(IsSet($field["default"]) ? " DEFAULT ".$this->GetFloatFieldValue($field["default"]) : "").(IsSet($field["notnull"]) ? " NOT NULL" : ""));
155: }
156:
157: Function GetDecimalFieldTypeDeclaration($name,&$field)
158: {
159: return("$name BIGINT".(IsSet($field["default"]) ? " DEFAULT ".$this->GetDecimalFieldValue($field["default"]) : "").(IsSet($field["notnull"]) ? " NOT NULL" : ""));
160: }
161:
162: Function GetFloatFieldValue($value)
163: {
164: return(!strcmp($value,"NULL") ? "NULL" : "$value");
165: }
166:
167: Function GetDecimalFieldValue($value)
168: {
169: return(!strcmp($value,"NULL") ? "NULL" : strval(intval($value*$this->decimal_factor)));
170: }
171:
172: Function AlterTable($name,$changes,$check)
173: {
174: if($check)
175: {
176: for($change=0,Reset($changes);$change<count($changes);Next($changes),$change++)
177: {
178: switch(Key($changes))
179: {
180: case "AddedFields":
181: case "RemovedFields":
182: case "ChangedFields":
183: case "RenamedFields":
184: case "name":
185: break;
186: default:
187: $this->last_error="change type \"".Key($changes)."\" not yet supported";
188: return(0);
189: }
190: }
191: return(1);
192: }
193: else
194: {
195: $query=(IsSet($changes["name"]) ? "RENAME AS ".$changes["name"] : "");
196: if(IsSet($changes["AddedFields"]))
197: {
198: $fields=$changes["AddedFields"];
199: for($field=0,Reset($fields);$field<count($fields);Next($fields),$field++)
200: {
201: if(strcmp($query,""))
202: $query.=", ";
203: $query.="ADD ".$fields[Key($fields)]["Declaration"];
204: }
205: }
206: if(IsSet($changes["RemovedFields"]))
207: {
208: $fields=$changes["RemovedFields"];
209: for($field=0,Reset($fields);$field<count($fields);Next($fields),$field++)
210: {
211: if(strcmp($query,""))
212: $query.=", ";
213: $query.="DROP ".Key($fields);
214: }
215: }
216: $renamed_fields=array();
217: if(IsSet($changes["RenamedFields"]))
218: {
219: $fields=$changes["RenamedFields"];
220: for($field=0,Reset($fields);$field<count($fields);Next($fields),$field++)
221: $renamed_fields[$fields[Key($fields)]["name"]]=Key($fields);
222: }
223: if(IsSet($changes["ChangedFields"]))
224: {
225: $fields=$changes["ChangedFields"];
226: for($field=0,Reset($fields);$field<count($fields);Next($fields),$field++)
227: {
228: if(strcmp($query,""))
229: $query.=", ";
230: if(IsSet($renamed_fields[Key($fields)]))
231: {
232: $field_name=$renamed_fields[Key($fields)];
233: UnSet($renamed_fields[Key($fields)]);
234: }
235: else
236: $field_name=Key($fields);
237: $query.="CHANGE $field_name ".$fields[Key($fields)]["Declaration"];
238: }
239: }
240: if(count($renamed_fields))
241: {
242: for($field=0,Reset($renamed_fields);$field<count($renamed_fields);Next($renamed_fields),$field++)
243: {
244: if(strcmp($query,""))
245: $query.=", ";
246: $old_field_name=$renamed_fields[Key($renamed_fields)];
247: $query.="CHANGE $old_field_name ".$changes["RenamedFields"][$old_field_name]["Declaration"];
248: }
249: }
250: return($this->Query("ALTER TABLE $name $query"));
251: }
252: }
253:
254: Function CreateSequence($name,$start)
255: {
256: if(!$this->Query("CREATE TABLE _sequence_$name (sequence INT DEFAULT '0' NOT NULL AUTO_INCREMENT, PRIMARY KEY (sequence))"))
257: return(0);
258: if($this->Query("INSERT INTO _sequence_$name (sequence) VALUES (".($start-1).")")
259: && $this->Query("UPDATE _sequence_$name SET sequence=".($start-1)))
260: return(1);
261: $error=$this->last_error;
262: if(!$this->Query("DROP TABLE _sequence_$name"))
263: $this->warning="could not drop inconsistent sequence table";
264: $this->last_error=$error;
265: return(0);
266: }
267:
268: Function DropSequence($name)
269: {
270: return($this->Query("DROP TABLE _sequence_$name"));
271: }
272:
273: Function GetSequenceNextValue($name,&$value)
274: {
275: if(!$this->Query("INSERT INTO _sequence_$name (sequence) VALUES (NULL)"))
276: return(0);
277: $value=intval(mysql_insert_id());
278: if(!$this->Query("DELETE FROM _sequence_$name WHERE sequence<$value"))
279: $this->warning="could delete previous sequence table values";
280: return(1);
281: }
282:
283: Function GetSequenceCurrentValue($name,&$value)
284: {
285: if(($result=$this->Query("SELECT MAX(sequence) FROM _sequence_$name"))==0)
286: return(0);
287: $value=intval($this->FetchResult($result,0,0));
288: $this->FreeResult($result);
289: return(1);
290: }
291:
292: Function CreateIndex($table,$name,$definition)
293: {
294: $query="ALTER TABLE $table ADD ".(IsSet($definition["unique"]) ? "UNIQUE" : "INDEX")." $name (";
295: for($field=0,Reset($definition["FIELDS"]);$field<count($definition["FIELDS"]);$field++,Next($definition["FIELDS"]))
296: {
297: if($field>0)
298: $query.=",";
299: $query.=Key($definition["FIELDS"]);
300: }
301: $query.=")";
302: return($this->Query($query));
303: }
304:
305: Function DropIndex($table,$name)
306: {
307: return($this->Query("ALTER TABLE $table DROP INDEX $name"));
308: }
309:
310: Function Setup()
311: {
312: $this->supported["Sequences"]=
313: $this->supported["Indexes"]=
314: $this->supported["AffectedRows"]=
315: $this->supported["SummaryFunctions"]=
316: $this->supported["OrderByText"]=
317: $this->supported["GetSequenceCurrentValue"]=
318: 1;
319: $this->decimal_factor=pow(1.0,$this->decimal_places);
320: return("");
321: }
322:
323: };
324:
325: }
326: ?>
|