| | 46 | * Create index of the specified table |
|---|
| | 47 | * @param db The SQLConnection of the specified DataBase |
|---|
| | 48 | * @param table The table to create index |
|---|
| | 49 | * @param indexDefs ArrayCollection of the index definition |
|---|
| | 50 | * and it contains the following params. |
|---|
| | 51 | * - name : Index name |
|---|
| | 52 | * - columns: The list of the column |
|---|
| | 53 | * @return true/false If it succeed to create the index |
|---|
| | 54 | */ |
|---|
| | 55 | static public function createIndex(db:SQLConnection, table:String, indexDefs:ArrayCollection):Boolean { |
|---|
| | 56 | var result:Boolean = true; |
|---|
| | 57 | var stmt:SQLStatement = new SQLStatement; |
|---|
| | 58 | stmt.sqlConnection = db; |
|---|
| | 59 | var sql:String = ""; |
|---|
| | 60 | for each(var val:* in indexDefs) { |
|---|
| | 61 | sql = "CREATE INDEX IF NOT EXISTS " + val.name + " ON " + table + "(" + val.columns + ")"; |
|---|
| | 62 | stmt.text = sql; |
|---|
| | 63 | try { |
|---|
| | 64 | stmt.execute(); |
|---|
| | 65 | } catch (err:Error) { |
|---|
| | 66 | Alert.show(err.toString(), "ERROR!:createIndex"); |
|---|
| | 67 | result = false; |
|---|
| | 68 | } |
|---|
| | 69 | } |
|---|
| | 70 | return result; |
|---|
| | 71 | } |
|---|
| | 72 | |
|---|
| | 73 | /** |
|---|
| | 74 | * Create table to the specified database |
|---|
| | 75 | * @param db The SQLConnection of the specified DataBase |
|---|
| | 76 | * @param table The table name |
|---|
| | 77 | * @param tableDefs ArrayCollection of the table definition |
|---|
| | 78 | * and it contains the following params. |
|---|
| | 79 | * - name : Column name |
|---|
| | 80 | * - type : The data type of the column |
|---|
| | 81 | * - primaryKey : If the column is primary key |
|---|
| | 82 | * - autoIncrement: If it will increment the value automatically |
|---|
| | 83 | * - allowNull : If the column allows null value |
|---|
| | 84 | * @return true/false If it succeed to create the table |
|---|
| | 85 | */ |
|---|
| | 86 | static public function createTable(db:SQLConnection, table:String, tableDefs:ArrayCollection):Boolean { |
|---|
| | 87 | var result:Boolean = true; |
|---|
| | 88 | var stmt:SQLStatement = new SQLStatement; |
|---|
| | 89 | stmt.sqlConnection = db; |
|---|
| | 90 | var sql:String = "CREATE TABLE IF NOT EXISTS " + table + " ("; |
|---|
| | 91 | for each(var val:* in tableDefs) { |
|---|
| | 92 | sql += val.name + " " + val.type; |
|---|
| | 93 | if (val.primaryKey) |
|---|
| | 94 | sql += " " + "PRIMARY KEY"; |
|---|
| | 95 | if (val.autoIncrement) |
|---|
| | 96 | sql += " " + "AUTOINCREMENT"; |
|---|
| | 97 | if (!val.allowNull) |
|---|
| | 98 | sql += " " + "NOT NULL"; |
|---|
| | 99 | sql += ", "; |
|---|
| | 100 | } |
|---|
| | 101 | stmt.text = sql.substr(0, sql.length - 2); |
|---|
| | 102 | stmt.text += ")"; |
|---|
| | 103 | try { |
|---|
| | 104 | stmt.execute(); |
|---|
| | 105 | } catch (err:Error) { |
|---|
| | 106 | Alert.show(err.toString(), "ERROR!:createTable"); |
|---|
| | 107 | result = false; |
|---|
| | 108 | } |
|---|
| | 109 | return result; |
|---|
| | 110 | } |
|---|
| | 111 | |
|---|
| | 112 | /** |
|---|
| | 113 | * Delete row from the specified table |
|---|
| | 114 | * @param db The SQLConnection of the specified DataBase |
|---|
| | 115 | * @param table The table to delete the row |
|---|
| | 116 | * @param keyColumn The column name that specify the row |
|---|
| | 117 | * @param keyValue The column value that specify the row |
|---|
| | 118 | * @return true/false If it succeed to delete the row |
|---|
| | 119 | */ |
|---|
| | 120 | static public function deleteRow(db:SQLConnection, table:String, keyColumn:String, keyValue:int):Boolean { |
|---|
| | 121 | var result:Boolean = false; |
|---|
| | 122 | var sql:String = "DELETE FROM " + table + " WHERE " + keyColumn + " = " + keyValue; |
|---|
| | 123 | try { |
|---|
| | 124 | var stmt:SQLStatement = new SQLStatement; |
|---|
| | 125 | stmt.sqlConnection = db; |
|---|
| | 126 | stmt.text = sql; |
|---|
| | 127 | stmt.execute(); |
|---|
| | 128 | } catch (err:Error) { |
|---|
| | 129 | Alert.show(err.toString(), "ERROR!:update"); |
|---|
| | 130 | result = false; |
|---|
| | 131 | } |
|---|
| | 132 | return result; |
|---|
| | 133 | |
|---|
| | 134 | } |
|---|
| | 135 | |
|---|
| | 136 | /** |
|---|
| | 137 | * Check if the index exists in the specified table |
|---|
| | 138 | * @param db The SQLConnection of the specified DataBase |
|---|
| | 139 | * @param table The table if contains the index |
|---|
| | 140 | * @param index The index name to check |
|---|
| | 141 | * @return true/false If the index exists in the table |
|---|
| | 142 | */ |
|---|
| | 143 | static public function existsIndex(db:SQLConnection, table:String, index:String):Boolean { |
|---|
| | 144 | var result:Boolean = false; |
|---|
| | 145 | var indexList:ArrayCollection = getIndexList(db, table); |
|---|
| | 146 | for each(var val:* in indexList) { |
|---|
| | 147 | if(val.name == index) { |
|---|
| | 148 | result = true; |
|---|
| | 149 | break; |
|---|
| | 150 | } |
|---|
| | 151 | } |
|---|
| | 152 | return result; |
|---|
| | 153 | } |
|---|
| | 154 | |
|---|
| | 155 | /** |
|---|
| | 156 | * Check if the table exists in the database |
|---|
| | 157 | * @param db The SQLConnection of the specified DataBase |
|---|
| | 158 | * @param table The table name to check |
|---|
| | 159 | * @return true/false If the table exists in the table |
|---|
| | 160 | */ |
|---|
| | 161 | static public function existsTable(db:SQLConnection, table:String):Boolean { |
|---|
| | 162 | var result:Boolean = false; |
|---|
| | 163 | var tableList:Array = getTableList(db); |
|---|
| | 164 | for each(var val:* in tableList) { |
|---|
| | 165 | if(val == table) { |
|---|
| | 166 | result = true; |
|---|
| | 167 | break; |
|---|
| | 168 | } |
|---|
| | 169 | } |
|---|
| | 170 | return result; |
|---|
| | 171 | } |
|---|
| | 172 | |
|---|
| | 173 | /** |
|---|
| | 174 | * Get the index list of the specified table |
|---|
| | 175 | * @param db The SQLConnection of the specified DataBase |
|---|
| | 176 | * @param table The table to get the list of index |
|---|
| | 177 | * @return The ArrayCollection of the index |
|---|
| | 178 | * and it contains the following params |
|---|
| | 179 | * - name : Index name |
|---|
| | 180 | * - columns: The column list |
|---|
| | 181 | */ |
|---|
| | 182 | static public function getIndexList(db:SQLConnection, table:String):ArrayCollection { |
|---|
| | 183 | var result:ArrayCollection = new ArrayCollection(); |
|---|
| | 184 | try { |
|---|
| | 185 | db.loadSchema(SQLIndexSchema, table); |
|---|
| | 186 | var sqlschema:SQLSchemaResult = db.getSchemaResult(); |
|---|
| | 187 | for each(var index:* in sqlschema.indices) { |
|---|
| | 188 | if (table == null) |
|---|
| | 189 | result.addItem({name: index.name, columns: ""}); |
|---|
| | 190 | else |
|---|
| | 191 | result.addItem({name: index.name, |
|---|
| | 192 | columns: index.sql.substring(index.sql.search("ON " + table) |
|---|
| | 193 | + table.length + 4, index.sql.length - 1)}); |
|---|
| | 194 | } |
|---|
| | 195 | } catch (err:Error) { |
|---|
| | 196 | //Alert.show(err.toString(), "ERROR!:getIndexList"); |
|---|
| | 197 | } |
|---|
| | 198 | return result; |
|---|
| | 199 | } |
|---|
| | 200 | |
|---|
| | 201 | /** |
|---|
| | 341 | } |
|---|
| | 342 | |
|---|
| | 343 | /** |
|---|
| | 344 | * Insert row to the specified table |
|---|
| | 345 | * @param db The SQLConnection of the specified DataBase |
|---|
| | 346 | * @param table The table to insert the data |
|---|
| | 347 | * @param rows The ArrayCollection of the data to insert |
|---|
| | 348 | * and it contains the following params |
|---|
| | 349 | * - column: Column name |
|---|
| | 350 | * - value : Column value |
|---|
| | 351 | * - type : Data type of the column |
|---|
| | 352 | * @return true/false If it succeed to insert the row data |
|---|
| | 353 | */ |
|---|
| | 354 | public static function insertRow(db:SQLConnection, table:String, rows:ArrayCollection):Boolean { |
|---|
| | 355 | var result:Boolean = true; |
|---|
| | 356 | var sql:String = "INSERT INTO " + table + "("; |
|---|
| | 357 | for each(var val:* in rows) |
|---|
| | 358 | sql += val.column + ", "; |
|---|
| | 359 | sql = sql.substring(0, sql.length - 2); |
|---|
| | 360 | sql += ") VALUES ("; |
|---|
| | 361 | for each(val in rows) { |
|---|
| | 362 | if (val.type == "TEXT") { |
|---|
| | 363 | sql += "'"; |
|---|
| | 364 | if (val.value != null) |
|---|
| | 365 | sql += val.value; |
|---|
| | 366 | sql += "'"; |
|---|
| | 367 | } else { |
|---|
| | 368 | sql += val.value; |
|---|
| | 369 | } |
|---|
| | 370 | sql += ", "; |
|---|
| | 371 | } |
|---|
| | 372 | sql = sql.substring(0, sql.length - 2); |
|---|
| | 373 | sql += ")"; |
|---|
| | 374 | try { |
|---|
| | 375 | var stmt:SQLStatement = new SQLStatement(); |
|---|
| | 376 | stmt.sqlConnection = db; |
|---|
| | 377 | stmt.text = sql; |
|---|
| | 378 | stmt.execute(); |
|---|
| | 379 | } catch (err:Error) { |
|---|
| | 380 | Alert.show(err.toString(), "ERROR!:insert"); |
|---|
| | 381 | result = false; |
|---|
| | 382 | } |
|---|
| | 383 | return result; |
|---|
| | 384 | } |
|---|
| | 385 | |
|---|
| | 386 | /** |
|---|
| | 387 | * Update row of the specified table |
|---|
| | 388 | * @param db The SQLConnection of the specified DataBase |
|---|
| | 389 | * @param table The table to update the data |
|---|
| | 390 | * @param keyColumn The column name to specify the row |
|---|
| | 391 | * @param keyValue The column value to specify the row |
|---|
| | 392 | * @param updateColumn The column name to update |
|---|
| | 393 | * @param updateValue The column value to update |
|---|
| | 394 | * @param updateType The data type of the column to update |
|---|
| | 395 | * @return true/false If it succeed to update the row data |
|---|
| | 396 | */ |
|---|
| | 397 | public static function updateRow(db:SQLConnection, table:String, keyColumn:String, keyValue:int, |
|---|
| | 398 | updateColumn:String, updateValue:String, updateType:String):Boolean { |
|---|
| | 399 | var result:Boolean = true; |
|---|
| | 400 | |
|---|
| | 401 | if (updateType == "TEXT") |
|---|
| | 402 | updateValue = "'" + updateValue + "'"; |
|---|
| | 403 | |
|---|
| | 404 | var sql:String = "UPDATE " + table + " SET " + updateColumn + " = " + updateValue |
|---|
| | 405 | + " WHERE " + keyColumn + " = " + keyValue; |
|---|
| | 406 | try { |
|---|
| | 407 | var stmt:SQLStatement = new SQLStatement; |
|---|
| | 408 | stmt.sqlConnection = db; |
|---|
| | 409 | stmt.text = sql; |
|---|
| | 410 | stmt.execute(); |
|---|
| | 411 | } catch (err:Error) { |
|---|
| | 412 | Alert.show(err.toString(), "ERROR!:update"); |
|---|
| | 413 | result = false; |
|---|
| | 414 | } |
|---|
| | 415 | return result; |
|---|