| 1 |
import flash.events.*; |
|---|
| 2 |
import mx.events.*; |
|---|
| 3 |
import mx.collections.ArrayCollection; |
|---|
| 4 |
import mx.controls.Alert; |
|---|
| 5 |
import flash.data.SQLConnection; |
|---|
| 6 |
import flash.filesystem.File; |
|---|
| 7 |
import mx.binding.utils.ChangeWatcher; |
|---|
| 8 |
import org.libspark.utils.SqlUtil; |
|---|
| 9 |
import org.libspark.snippets.controls.SQLTableGrid; |
|---|
| 10 |
|
|---|
| 11 |
[Bindable] |
|---|
| 12 |
public var tableDefs:ArrayCollection = new ArrayCollection([ |
|---|
| 13 |
{name: "record_id", type: "INTEGER", primaryKey: true, autoIncrement: false, allowNull: false} |
|---|
| 14 |
]); |
|---|
| 15 |
|
|---|
| 16 |
[Bindable] |
|---|
| 17 |
public var indexDefs:ArrayCollection = new ArrayCollection(); |
|---|
| 18 |
|
|---|
| 19 |
[Bindable] |
|---|
| 20 |
public var columnList:ArrayCollection = new ArrayCollection([{label: "record_id"}]); |
|---|
| 21 |
|
|---|
| 22 |
[Bindable] |
|---|
| 23 |
public var indexList:ArrayCollection = new ArrayCollection(); |
|---|
| 24 |
|
|---|
| 25 |
private static const typeData:Array = new Array("INTEGER", "FLOAT", "TEXT", "BLOB"); |
|---|
| 26 |
|
|---|
| 27 |
private var connection:SQLConnection; |
|---|
| 28 |
|
|---|
| 29 |
private function bAddColumn_click(event:MouseEvent):void { |
|---|
| 30 |
tableDefs.addItem( |
|---|
| 31 |
{name: "", type: "TEXT", primaryKey: false, autoIncrement: false, allowNull: true} |
|---|
| 32 |
); |
|---|
| 33 |
} |
|---|
| 34 |
|
|---|
| 35 |
private function bDelColumn_click(event:MouseEvent):void { |
|---|
| 36 |
if (dg.selectedIndex >= 0) |
|---|
| 37 |
tableDefs.removeItemAt(dg.selectedIndex); |
|---|
| 38 |
} |
|---|
| 39 |
|
|---|
| 40 |
private function bDelIndex_click(event:MouseEvent):void { |
|---|
| 41 |
if (dgIndexList.selectedIndex >= 0) |
|---|
| 42 |
indexDefs.removeItemAt(dgIndexList.selectedIndex); |
|---|
| 43 |
} |
|---|
| 44 |
|
|---|
| 45 |
private function bCancelColumnIndex_click(event:MouseEvent):void { |
|---|
| 46 |
indexList.removeAll(); |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
private function bAddIndex_click(event:MouseEvent):void { |
|---|
| 50 |
if (tIndex.text == "") { |
|---|
| 51 |
Alert.show("Please input index name", "CONFIRM"); |
|---|
| 52 |
return; |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
if (SqlUtil.existsIndex(connection, null, tIndex.text)) { |
|---|
| 56 |
Alert.show("The index " + tIndex.text + " already exists in database. Please change the index name." , "CONFIRM"); |
|---|
| 57 |
} else { |
|---|
| 58 |
var indexColumns:String = ""; |
|---|
| 59 |
for each(var val:* in indexList) |
|---|
| 60 |
indexColumns += val + ", "; |
|---|
| 61 |
indexColumns = indexColumns.substr(0, indexColumns.length - 2); |
|---|
| 62 |
indexDefs.addItem({name: tIndex.text, columns: indexColumns}); |
|---|
| 63 |
tIndex.text = ""; |
|---|
| 64 |
indexList.removeAll(); |
|---|
| 65 |
} |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
private function bCreateTable_click(event:MouseEvent):void { |
|---|
| 69 |
if (tTable.text == "") { |
|---|
| 70 |
Alert.show("Please input table name", "CONFIRM"); |
|---|
| 71 |
return; |
|---|
| 72 |
} |
|---|
| 73 |
if(SqlUtil.existsTable(connection, tTable.text)) { |
|---|
| 74 |
Alert.show("Table " + tTable.text + " already exists in database. Please change the table name.", "CONFIRM"); |
|---|
| 75 |
} else { |
|---|
| 76 |
var status:Boolean = SqlUtil.createTable(connection, tTable.text, tableDefs); |
|---|
| 77 |
if (status) { |
|---|
| 78 |
status = SqlUtil.createIndex(connection, tTable.text, indexDefs); |
|---|
| 79 |
Alert.show("Table " + tTable.text + " created", "CONFIRM"); |
|---|
| 80 |
tTable.text = ""; |
|---|
| 81 |
indexList.removeAll(); |
|---|
| 82 |
indexDefs.removeAll(); |
|---|
| 83 |
} |
|---|
| 84 |
} |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
private function bCancel_click(event:MouseEvent):void { |
|---|
| 88 |
this.close(); |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
private function onTableColumnChange(event:Event):void { |
|---|
| 92 |
columnList.removeAll(); |
|---|
| 93 |
indexList.removeAll(); |
|---|
| 94 |
for each (var val:* in tableDefs) |
|---|
| 95 |
columnList.addItem(val.name); |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
private var dg:SQLTableGrid; |
|---|
| 99 |
|
|---|
| 100 |
private function init():void { |
|---|
| 101 |
addEventListener(Event.CLOSING, onClosing); |
|---|
| 102 |
tableDefs.addEventListener(CollectionEvent.COLLECTION_CHANGE, onTableColumnChange); |
|---|
| 103 |
dg = new SQLTableGrid(true); |
|---|
| 104 |
dg.x = 20; dg.y = 75; dg.width = 360; dg.height = 180; |
|---|
| 105 |
dg.dataProvider = tableDefs; |
|---|
| 106 |
dg.editable = true; |
|---|
| 107 |
addChild(dg); |
|---|
| 108 |
connection = new SQLConnection(); |
|---|
| 109 |
openDB(); |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
private function openDB():void { |
|---|
| 113 |
var dbFile:File = new File(); |
|---|
| 114 |
var dbFilter:FileFilter = new FileFilter("DB File", "*.db"); |
|---|
| 115 |
try { |
|---|
| 116 |
dbFile.browseForOpen("Open", [dbFilter]); |
|---|
| 117 |
dbFile.addEventListener(Event.SELECT, dbFileSelected); |
|---|
| 118 |
dbFile.addEventListener(Event.CANCEL, dbFileCanceled); |
|---|
| 119 |
} catch (error:Error) { |
|---|
| 120 |
Alert.show(error.message, "ERROR!"); |
|---|
| 121 |
} |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|
| 124 |
private function dbFileSelected(event:Event):void { |
|---|
| 125 |
try { |
|---|
| 126 |
connection.open(File(event.target), SQLMode.READ); |
|---|
| 127 |
dg.sqlConnection = connection; |
|---|
| 128 |
} catch(error:Error) { |
|---|
| 129 |
Alert.show(error.message, "ERROR!"); |
|---|
| 130 |
} |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
private function dbFileCanceled(event:Event):void { |
|---|
| 134 |
try { |
|---|
| 135 |
var path:String = "file://" + File.documentsDirectory.nativePath + "/user.db"; |
|---|
| 136 |
connection.open(new File(path), SQLMode.CREATE); |
|---|
| 137 |
dg.sqlConnection = connection; |
|---|
| 138 |
} catch(error:Error) { |
|---|
| 139 |
Alert.show(error.message, "ERROR!"); |
|---|
| 140 |
} |
|---|
| 141 |
} |
|---|
| 142 |
|
|---|
| 143 |
private function onClosing(event:Event):void { |
|---|
| 144 |
trace("Application closing..."); |
|---|
| 145 |
if (connection != null) |
|---|
| 146 |
if(connection.connected) |
|---|
| 147 |
connection.close(); |
|---|
| 148 |
} |
|---|