| 1 | package com.gnstudio.nabiro.air.data.utils
|
|---|
| 2 | {
|
|---|
| 3 |
|
|---|
| 4 | /**
|
|---|
| 5 | *
|
|---|
| 6 | * GNstudio nabiro
|
|---|
| 7 | * =====================================================================
|
|---|
| 8 | * Copyright(c) 2009
|
|---|
| 9 | * http://www.gnstudio.com
|
|---|
| 10 | *
|
|---|
| 11 | *
|
|---|
| 12 | *
|
|---|
| 13 | * This file is part of the nabiro flash platform framework
|
|---|
| 14 | *
|
|---|
| 15 | *
|
|---|
| 16 | * nabiro is free software; you can redistribute it and/or modify
|
|---|
| 17 | * it under the terms of the GNU Lesser General Public License as published by
|
|---|
| 18 | * the Free Software Foundation; either version 3 of the License, or
|
|---|
| 19 | * at your option) any later version.
|
|---|
| 20 | *
|
|---|
| 21 | * nabiro is distributed in the hope that it will be useful,
|
|---|
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 24 | * GNU General Public License for more details.
|
|---|
| 25 | *
|
|---|
| 26 | * You should have received a copy of the GNU Lesser General Public License
|
|---|
| 27 | * along with Intelligere SCS; if not, write to the Free Software
|
|---|
| 28 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|---|
| 29 | * =====================================================================
|
|---|
| 30 | *
|
|---|
| 31 | *
|
|---|
| 32 | *
|
|---|
| 33 | * @package nabiro
|
|---|
| 34 | *
|
|---|
| 35 | * @version 0.9
|
|---|
| 36 | * @idea maker Giorgio Natili [ g.natili@gnstudio.com ]
|
|---|
| 37 | * @author Giorgio Natili [ g.natili@gnstudio.com ]
|
|---|
| 38 | *
|
|---|
| 39 | *
|
|---|
| 40 | */
|
|---|
| 41 |
|
|---|
| 42 | import com.gnstudio.nabiro.air.data.events.SQLHashEvent;
|
|---|
| 43 |
|
|---|
| 44 | import flash.data.SQLConnection;
|
|---|
| 45 | import flash.events.Event;
|
|---|
| 46 | import flash.events.EventDispatcher;
|
|---|
| 47 | import flash.events.IEventDispatcher;
|
|---|
| 48 | import flash.utils.Dictionary;
|
|---|
| 49 |
|
|---|
| 50 | /**
|
|---|
| 51 | *
|
|---|
| 52 | * <code>SQLConnectionHash</code> is class which provides
|
|---|
| 53 | * a centralized location from which all <code>SQLConnection</code> instances
|
|---|
| 54 | * can be managed and retrieved.
|
|---|
| 55 | *
|
|---|
| 56 | * <p>
|
|---|
| 57 | * <code>SQLConnectionHelper</code> utilizes the <code>nativePath</code>
|
|---|
| 58 | * property of a <code>File</code> object used to create the connection.
|
|---|
| 59 | * This is utilized to uniquely identify each cached <code>SQLConnection</code>
|
|---|
| 60 | * database instance within the connections map.
|
|---|
| 61 | * </p>
|
|---|
| 62 | *
|
|---|
| 63 | */
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 | public class SQLConnectionHash implements IEventDispatcher{
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 | private var eventDispatcher:EventDispatcher = new EventDispatcher();
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 | /**
|
|---|
| 73 | *
|
|---|
| 74 | * Contains a mapping of each unique <code>SQLConnection</code> instance
|
|---|
| 75 | *
|
|---|
| 76 | */
|
|---|
| 77 | private const connections:Dictionary = new Dictionary(true);
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 | public function SQLConnectionHash(){
|
|---|
| 82 |
|
|---|
| 83 | eventDispatcher = new EventDispatcher();
|
|---|
| 84 |
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | /**
|
|---|
| 88 | *
|
|---|
| 89 | * Retrieves a managed <code>SQLConnection</code> instance based on the
|
|---|
| 90 | * unique name of the connection
|
|---|
| 91 | *
|
|---|
| 92 | * @example The following example demonstrates how <code>SQLConnectionHelper</code>
|
|---|
| 93 | * can be utilized to retrieve a reference to a shared <code>SQLConnection</code>
|
|---|
| 94 | *
|
|---|
| 95 | * @param unique name of the <code>SQLConnection</code>
|
|---|
| 96 | * @return managed <code>SQLConnection</code> instance
|
|---|
| 97 | *
|
|---|
| 98 | */
|
|---|
| 99 | public function getConnection(name:String) : SQLConnection{
|
|---|
| 100 |
|
|---|
| 101 | var connection:SQLConnection;
|
|---|
| 102 |
|
|---|
| 103 | for (var k:* in connections){
|
|---|
| 104 |
|
|---|
| 105 | if(k == name){
|
|---|
| 106 |
|
|---|
| 107 | connection = connections[k];
|
|---|
| 108 |
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | if (connection == null){
|
|---|
| 114 |
|
|---|
| 115 | connection = new SQLConnection();
|
|---|
| 116 | connections[ name ] = connection;
|
|---|
| 117 |
|
|---|
| 118 | dispatchEvent(new SQLHashEvent(SQLHashEvent.EMPTY_CONNECTION_CREATED, name, connection));
|
|---|
| 119 |
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | return connection;
|
|---|
| 123 |
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | /**
|
|---|
| 127 | * Add a connection to the hashmap checking if a connection with this name already exists
|
|---|
| 128 | *
|
|---|
| 129 | * @param name String
|
|---|
| 130 | * @param connection SQLConnection
|
|---|
| 131 | *
|
|---|
| 132 | */
|
|---|
| 133 |
|
|---|
| 134 | public function addConnection(name:String, connection:SQLConnection):void{
|
|---|
| 135 |
|
|---|
| 136 | for (var k:* in connections){
|
|---|
| 137 |
|
|---|
| 138 | if(k == name){
|
|---|
| 139 |
|
|---|
| 140 | throw new Error("A connection with the name " + name + " already exist");
|
|---|
| 141 | break;
|
|---|
| 142 |
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | connections[name] = connection;
|
|---|
| 148 |
|
|---|
| 149 | dispatchEvent(new SQLHashEvent(SQLHashEvent.CONNECTION_ADDED, name, connection));
|
|---|
| 150 |
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | /**
|
|---|
| 154 | *
|
|---|
| 155 | * Closes the connection to the previously cached <code>SQLConnection</code>
|
|---|
| 156 | * instance and remove it from the map
|
|---|
| 157 | *
|
|---|
| 158 | * @param The database name of the <code>SQLConnection</code>
|
|---|
| 159 | *
|
|---|
| 160 | */
|
|---|
| 161 | public function closeConnection(name:String) : void{
|
|---|
| 162 |
|
|---|
| 163 | var connection:SQLConnection;
|
|---|
| 164 |
|
|---|
| 165 | for (var k:* in connections){
|
|---|
| 166 |
|
|---|
| 167 | if(k == name){
|
|---|
| 168 |
|
|---|
| 169 | connection = connections[k];
|
|---|
| 170 | break;
|
|---|
| 171 |
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | if ( connection != null ){
|
|---|
| 177 |
|
|---|
| 178 | if ( connection.connected && !connection.inTransaction ){
|
|---|
| 179 |
|
|---|
| 180 | connection.close();
|
|---|
| 181 | delete connections[k];
|
|---|
| 182 |
|
|---|
| 183 | dispatchEvent(new SQLHashEvent(SQLHashEvent.CONNECTION_CLOSE_SUCCESS, name, connection));
|
|---|
| 184 |
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | }else{
|
|---|
| 188 |
|
|---|
| 189 | dispatchEvent(new SQLHashEvent(SQLHashEvent.CONNECTION_CLOSE_ERROR, name));
|
|---|
| 190 |
|
|---|
| 191 | }
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | /************************************
|
|---|
| 195 | * IEventDispatcher immplementation
|
|---|
| 196 | *************************************/
|
|---|
| 197 | public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0.0, useWeakReference:Boolean=false):void{
|
|---|
| 198 |
|
|---|
| 199 | eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
|
|---|
| 200 |
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void {
|
|---|
| 204 |
|
|---|
| 205 | eventDispatcher.removeEventListener(type, listener, useCapture);
|
|---|
| 206 |
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | public function dispatchEvent(event:Event):Boolean {
|
|---|
| 210 |
|
|---|
| 211 | return eventDispatcher.dispatchEvent(event);
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | public function hasEventListener(type:String):Boolean {
|
|---|
| 215 |
|
|---|
| 216 | return eventDispatcher.hasEventListener(type);
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | public function willTrigger(type:String):Boolean {
|
|---|
| 220 |
|
|---|
| 221 | return eventDispatcher.willTrigger(type);
|
|---|
| 222 |
|
|---|
| 223 | }
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 | } |
|---|