1 //!-- UTF8 2 /* 3 Oregano Multiuser Server - Version 1.2.0 - January 4th, 2005 4 5 Web: www.oregano-server.org 6 Mail: info@oregano-server.org 7 8 Copyright 2003 - 2004 - 2004 Jens Halm / Cologne, Germany 9 10 This library is free software; you can redistribute it and/or 11 modify it under the terms of the GNU Lesser General Public 12 License as published by the Free Software Foundation; either 13 version 2.1 of the License, or (at your option) any later version. 14 15 This library is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 Lesser General Public License for more details. 19 20 You should have received a copy of the GNU Lesser General Public 21 License along with this library; if not, write to the Free Software 22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 */ 24 25 /* 26 ------------------------------------------- 27 Classe Message 28 29 @description : 30 Donne une représentation du schéma de l'objet Table. 31 32 @author Jens Halm copyright http://www.spicefactory.org/ 33 @author erixtekila copyleft http://www.v-i-a.net 34 ------------------------------------------- 35 version history : 36 1.2.0 : 03/02/05 37 - Portage en actionscript 2 pour le 38 compile time type checking 39 - Nouvelle méthode d'accès à error de la classe Table 40 ------------------------------------------- 41 */ 42 43 import org.omus.util._Class; 44 45 /** 46 * Donne une représentaion du schéma de l'objet Table : 47 * ces colonnes, enregistrements… 48 * Définit les noms et type de données des colonnes d'une table. 49 * 50 * @see org.omus.data.Table 51 * 52 * @author Jens Halm copyright http://www.spicefactory.org/ 53 * @author erixtekila copyleft http://www.v-i-a.net 54 * @version 1.2.0 55 */ 56 class org.omus.data.TableDefinition 57 { 58 //-------------------- 59 // PROPRIETES 60 //-------------------- 61 /** 62 * Liste des colonnes du tableau associé. 63 */ 64 private var columns:Array; 65 66 /** 67 * Un conteneur des types hébergés. 68 */ 69 private var typeMap:Object; 70 71 /** 72 * Modification possible. 73 */ 74 private var modifiable:Boolean; 75 76 /** 77 * Le code d'une erreur. 78 */ 79 private var error:String; 80 81 //-------------------- 82 // CONSTRUCTEUR 83 //-------------------- 84 /** 85 * Définit les noms et type de données des colonnes d'une table. 86 * 87 * @see org.omus.data.Table 88 */ 89 public function TableDefinition() 90 { 91 columns = new Array(); 92 typeMap = new Object(); 93 modifiable = true; 94 95 // trace(this+ " installé."); 96 } 97 98 //-------------------- 99 // METHODES PUBLIQUES 100 //-------------------- 101 /** 102 * Utilisé dans un contexte littéral 103 * @return Une chaine définissant l'objet 104 */ 105 public function toString():String 106 { 107 var s = "[Object DataParser]"; 108 s += "\norg.omus.TableDefinition - columns:"; 109 var col = columns; 110 var len = col.length; 111 for (var i = 0; i < len; i++) { 112 var entry = col[i]; 113 s += "\n[" + i + "] name = " + entry.name + " - type = " + entry.type; 114 } 115 return s; 116 } 117 118 /** 119 * Rajoute une colonne au schema de la Table 120 * 121 * @param name Nom de la colonne. 122 * @param type Type contenu dans la colonne. Les types permis sont : 123 * boolean, int, long, float, string, date, array, object, table 124 */ 125 public function addColumn (name:String, type:String):Void 126 { 127 // format for type -> float, int, long, table, array.... 128 if (! modifiable) 129 { 130 // Logs internes 131 _global.oregano.iLog.error("clj-033","column name = " + name + " - column type = " + type); 132 return; 133 } 134 // TODO : Accès Singleton 135 var clazz = _Class.getInstance(); 136 if (! clazz.checkArguments("org.omus.data.TableDefinition.addColumn", [[name, "string", true], [type, "string", true]])) return; 137 if (name.length == 0) 138 { 139 // Logs internes 140 _global.oregano.iLog.error("clj-034","column name = " + name + " - column type = " + type); 141 return; 142 } 143 if (typeof(typeMap[name]) != "undefined") 144 { 145 // Logs internes 146 _global.oregano.iLog.error("clj-035","column name = " + name + " - column type = " + type); 147 return; 148 } 149 var cl = clazz.cellTypeToClass(type); 150 if (typeof(cl) == "undefined") 151 { 152 // Logs internes 153 _global.oregano.iLog.error("clj-036","column name = " + name + " - column type = " + type); 154 return; 155 } 156 typeMap[name] = cl; 157 columns.push({name:name, type:type}); 158 } 159 160 /** 161 * Renvoie le nombre de colonnes définies 162 * 163 * @return Le nombre de colonne gérées pas TableDefinition 164 */ 165 public function getColumnCount ():Number { 166 return columns.length; 167 } 168 169 /** 170 * Renvoie le nom d'une colonne. 171 * 172 * @param index La position de la colonne. 173 * @return Le nom de la colonne. 174 */ 175 public function getColumnName (index:Number):String 176 { 177 if (typeof(index) != "number" || index < 0 || index >= columns.length) return null; 178 return columns[index].name; 179 } 180 181 /** 182 * Renvoie le type du contenu d'une colonne. 183 * 184 * @param index La position de la colonne.$ 185 * @return Le type de la colonne. 186 */ 187 public function getColumnType (index:Number):String 188 { 189 if (typeof(index) != "number" || index < 0 || index >= columns.length) return null; 190 return columns[index].type; 191 } 192 193 /** 194 * Renvoie une liste de tout les noms des colonnes. 195 */ 196 public function getColumnNames ():Array 197 { 198 var arr = new Array(); 199 var col = columns; 200 var l = col.length; 201 for (var i = 0; i < l; i++) 202 { 203 arr.push(col[i].name); 204 } 205 return arr; 206 } 207 208 /** 209 * Préviens de la concordance entre un enregiustrement et la définition d'une Table. 210 * 211 * @param row L'objet à vérifier. 212 * @return true si le nombre de colonne correspond au nombre de colonnes et si les types correspondent. 213 */ 214 public function matches (row:Object):Boolean 215 { 216 if (typeof(row) != "object") 217 { 218 error = "row is not an object"; 219 return false; 220 } 221 var cnt = 0; 222 for (var each:String in row) 223 { 224 var type = typeMap[each]; 225 if (typeof(type) == "undefined") 226 { 227 error = "unknown column name: " + each; 228 return false; 229 } 230 // ! TODO : instanceof 231 if (! _Class.isInstance (row[each], type)) 232 { 233 error = "column " + each + " has wrong datatype\nrequired: " + type + "\n found: " + typeof(row[each]); 234 return false; 235 } 236 cnt++; 237 } 238 if (cnt != columns.length) 239 { 240 error = "number of properties does not match"; 241 return false; 242 } 243 return true; 244 } 245 246 /** 247 * Interdit la modification. 248 */ 249 public function lock ():Void 250 { 251 modifiable = false; 252 } 253 254 /** 255 * Renvoie l'erreur associée 256 * Requis uniquement par la classe Table. 257 */ 258 public function getError():String 259 { 260 return error; 261 } 262 263 //-------------------- 264 // METHODES PRIVEES 265 //-------------------- 266 267 //-------------------- 268 // METHODES STATIQUES 269 //-------------------- 270 /** 271 * Utilisé dans un contexte littéral 272 * @return Une chaine définissant l'objet 273 */ 274 public static function toLog():String 275 { 276 return "[Object TableDefinition]"; 277 } 278 } 279