RFC - Sqlite and DFL

Tyro ridimz at yahoo.com
Fri May 25 01:59:22 PDT 2007


/*
	Generated by Entice Designer
	Entice Designer written by Christopher E. Miller
	www.dprogramming.com/entice.php
*/

private import dfl.all;
private import sqlite_import, sqlite_oo;

public import std.string, std.file, std.ctype;
import std.stream: File, FileMode;

pragma(lib, "sqlite.lib");

class idsMain: dfl.form.Form
{
	// Do not modify or move this block of variables.
	//~Entice Designer variables begin here.
	dfl.groupbox.GroupBox groupBox1;
	dfl.textbox.TextBox txtId;
	dfl.textbox.TextBox txtTam;
	dfl.textbox.TextBox txtNsn;
	dfl.label.Label lbID;
	dfl.label.Label lbTam;
	dfl.label.Label lbNSN;
	dfl.button.Button btn1;
	dfl.button.Button btnMares;
	dfl.button.Button button2;
	dfl.button.Button button3;
	dfl.listview.ListView lview;
	//~Entice Designer variables end here.
	
	SqliteDatabase db;
	SqliteResult res;
	int rec;
	
	int iord = 0, tord = 0, nord = 0;
	
	this()
	{
		initializeIdsMain();
		
		// Other idsMain initialization code here.
		initDesign();
		initDatabase();
		getContent(lview);
	}
	
	
	private void initializeIdsMain()
	{
		// Do not manually modify this function.
		//~Entice Designer 0.8.2 code begins here.
		//~DFL Form
		maximizeBox = false;
		minimizeBox = false;
		text = "ID Standard File Viewer";
		clientSize = dfl.drawing.Size(592, 574);
		//~DFL dfl.groupbox.GroupBox=groupBox1
		groupBox1 = new dfl.groupbox.GroupBox();
		groupBox1.name = "groupBox1";
		groupBox1.dock = dfl.control.DockStyle.TOP;
		groupBox1.bounds = dfl.base.Rect(0, 0, 592, 116);
		groupBox1.parent = this;
		//~DFL dfl.textbox.TextBox=txtId
		txtId = new dfl.textbox.TextBox();
		txtId.name = "txtId";
		txtId.bounds = dfl.base.Rect(60, 17, 64, 23);
		txtId.parent = groupBox1;
		//~DFL dfl.textbox.TextBox=txtTam
		txtTam = new dfl.textbox.TextBox();
		txtTam.name = "txtTam";
		txtTam.bounds = dfl.base.Rect(60, 49, 64, 23);
		txtTam.parent = groupBox1;
		//~DFL dfl.textbox.TextBox=txtNsn
		txtNsn = new dfl.textbox.TextBox();
		txtNsn.name = "txtNsn";
		txtNsn.bounds = dfl.base.Rect(60, 81, 120, 23);
		txtNsn.parent = groupBox1;
		//~DFL dfl.label.Label=lbID
		lbID = new dfl.label.Label();
		lbID.name = "lbID";
		lbID.text = "ID #:";
		lbID.textAlign = dfl.base.ContentAlignment.MIDDLE_LEFT;
		lbID.bounds = dfl.base.Rect(12, 17, 36, 23);
		lbID.parent = groupBox1;
		//~DFL dfl.label.Label=lbTam
		lbTam = new dfl.label.Label();
		lbTam.name = "lbTam";
		lbTam.text = "TAM #:";
		lbTam.textAlign = dfl.base.ContentAlignment.MIDDLE_LEFT;
		lbTam.bounds = dfl.base.Rect(12, 49, 44, 23);
		lbTam.parent = groupBox1;
		//~DFL dfl.label.Label=lbNSN
		lbNSN = new dfl.label.Label();
		lbNSN.name = "lbNSN";
		lbNSN.text = "NSN :";
		lbNSN.textAlign = dfl.base.ContentAlignment.MIDDLE_LEFT;
		lbNSN.bounds = dfl.base.Rect(12, 81, 36, 23);
		lbNSN.parent = groupBox1;
		//~DFL dfl.button.Button=btn1
		btn1 = new dfl.button.Button();
		btn1.name = "btn1";
		btn1.text = "Reload IDFile";
		btn1.bounds = dfl.base.Rect(468, 16, 83, 23);
		btn1.parent = groupBox1;
		//~DFL dfl.button.Button=btnMares
		btnMares = new dfl.button.Button();
		btnMares.name = "btnMares";
		btnMares.text = "MCBul3000 vs MARE IND";
		btnMares.bounds = dfl.base.Rect(212, 15, 155, 23);
		btnMares.parent = groupBox1;
		//~DFL dfl.button.Button=button2
		button2 = new dfl.button.Button();
		button2.name = "button2";
		button2.text = "MCBul3000 = N (MARE)";
		button2.bounds = dfl.base.Rect(212, 47, 155, 23);
		button2.parent = groupBox1;
		//~DFL dfl.button.Button=button3
		button3 = new dfl.button.Button();
		button3.name = "button3";
		button3.bounds = dfl.base.Rect(212, 79, 155, 23);
		button3.parent = groupBox1;
		//~DFL dfl.listview.ListView=lview
		lview = new dfl.listview.ListView();
		lview.name = "lview";
		lview.backColor = dfl.drawing.Color(255, 255, 0);
		lview.dock = dfl.control.DockStyle.BOTTOM;
		lview.font = new dfl.drawing.Font("Courier New", 10f, dfl.drawing.FontStyle.BOLD);
		lview.foreColor = dfl.drawing.Color(255, 0, 0);
		lview.fullRowSelect = true;
		lview.gridLines = true;
		lview.hideSelection = false;
		lview.multiSelect = false;
		lview.view = dfl.base.View.DETAILS;
		lview.bounds = dfl.base.Rect(0, 119, 592, 455);
		lview.parent = this;
		//~Entice Designer 0.8.2 code ends here.
	}
	
	private void initDesign()
	{
		ColumnHeader col;
		/* this is used to store the mysql row id */
			col = new ColumnHeader;
			col.width=0;
		lview.columns.add(col);
			col = new ColumnHeader;
			col.text = "ID";
			col.width= 60;
		lview.columns.add(col);
			col = new ColumnHeader;
			col.text = "TAM";
			col.width= 60;
		lview.columns.add(col);
			col = new ColumnHeader;
			col.text = "NSN";
			col.width= 120;
		lview.columns.add(col);
			col = new ColumnHeader;
			col.text = "NOMEN";
			col.width= 130;
		lview.columns.add(col);
			col = new ColumnHeader;
			col.text = "WSC";
			col.textAlign = dfl.base.HorizontalAlignment.CENTER;
			col.width= 50;
		lview.columns.add(col);
			col = new ColumnHeader;
			col.text = "FA";
			col.textAlign = dfl.base.HorizontalAlignment.CENTER;
			col.width= 50;
		lview.columns.add(col);
			col = new ColumnHeader;
			col.text = "EOTC";
			col.textAlign = dfl.base.HorizontalAlignment.CENTER;
			col.width= 50;
		lview.columns.add(col);
			col = new ColumnHeader;
			col.text = "MARE";
			col.textAlign = dfl.base.HorizontalAlignment.CENTER;
			col.width= 50;
		lview.columns.add(col);
		
		lview.columnClick ~= &lview_columnClick;
	}
	
	private void initDatabase()
	{
		db = new SqliteDatabase("ids.db");
		res = db.query("SELECT * FROM sqlite_master WHERE type='table' AND name='idstd'");
		if(!res.numRows)
		{
			db.exec("CREATE table idstd (
				idx integer primary key,
				id,
				tam,
				nsn,
				nomen,
				wsc,
				fa,
				eotc,
				mare);"
			);
			
			// Load ID Standards File if no db exists
			char[] idfile = cast(char[])read("idfile.ids");
			
			db.exec("BEGIN");
			while(idfile.length)
			{
				if(!isascii(idfile[11])) {
					idfile[11 .. 24] = '*';
					continue;
				}
				db.exec("INSERT INTO idstd
					values( null,
					'"~idfile[0..6]~"',   '"~ idfile[6..11] ~"',
					'"~ idfile[11..24] ~"', '"~ idfile[24..38] ~"',
					'"~ idfile[38..40] ~"', '"~ idfile[40..42] ~"',
					'"~ idfile[42..43] ~"', '"~ idfile[43..44] ~"'
				)");
				idfile = idfile[80..$];
			}
			db.exec("COMMIT");
		}
		/+
		This code works in when it is the only table being loaded
		but does crashes when moved to this program table.
		Order of execution does not matter! Always crashes on
		load of second table.
		
		// Load Marine Corps Bulliten 3000

		db.exec("CREATE table mcbul (
			idx integer primary key,
			tam,
			fa,
			wsc,
			nomen,
			mee);"
		);
		
		File mcbul = new File("mcbul3000.txt", FileMode.In);
		
		char[] buf;
		char[][] rec;
	
		db.exec("BEGIN");
		while((buf = mcbul.readLine) != null)
		{
			rec = split(buf, "\t");
			db.exec("INSERT INTO mcbul
				values( null,
				'"~rec[0]~"',   '"~ rec[1] ~"',
				'"~ rec[2] ~"', '"~ rec[3] ~"',
				'"~ rec[4] ~"'
			)");
			writefln(rec);
		}
		db.exec("COMMIT");
		mcbul.close();
		+/
	}
	
	private void getContent(ListView lv, char[] ord = "")
	{
		lv.clear();
		res = db.query("SELECT * FROM idstd"~ord);
		char[][] c;
		while((c = res.fetch()) != null)
		{
			lv.addRow(c);
		}
	}
	
	/+
	// Activly search for ID, TAM, or NSN
	/*
		As the user types a value in one of the three fields
		change focus to the closest matching value in that field.
		If after pressing the enter key the value does not match
		that of value selected, display Item Not Found message. 
	*/
	private void search(char[] value)
	{
		// Not sure how to implement this just yet
	}
	+/
	
	private void lview_columnClick(ListView sender, ColumnClickEventArgs ea)
	{
		switch(ea.column)
		{
			case 1: // Sort by ID number in ascending or descending order.
			{
				if (iord == 0)
				{
					lview.hide;
					getContent(lview, " ORDER BY id ASC");
					iord = 1;
					lview.show;
				}
				else
				{
					lview.hide;
					getContent(lview, " ORDER BY id DESC");
					iord = 0;
					lview.show;
				}
				break;
			}
			case 2: // Sort by TAM number in ascending or descending order.
			{
				if (tord == 0)
				{
					lview.hide;
					getContent(lview, " ORDER BY tam ASC");
					tord = 1;
					lview.show;
				}
				else
				{
					lview.hide;
					getContent(lview, " ORDER BY tam DESC");
					tord = 0;
					lview.show;
				}
				break;
			}
			
			case 3: // Sort by NSN in ascending or descending order.
			{
				if (nord == 0)
				{
					lview.hide;
					getContent(lview, " ORDER BY nsn ASC");
					nord = 1;
					lview.show;
				}
				else
				{
					lview.hide;
					getContent(lview, " ORDER BY nsn DESC");
					nord = 0;
					lview.show;
				}
				break;
			}
			
			case 4,5,6,7,8: break;
			default:
				assert(0);
		}
	}
}

int main()
{
	int result = 0;
	
	try
	{
		// Application initialization code here.
		
		Application.run(new idsMain());
	}
	catch(Object o)
	{
		msgBox(o.toString(), "Fatal Error", MsgBoxButtons.OK, MsgBoxIcon.ERROR);
		
		result = 1;
	}
	
	return result;
}



More information about the Digitalmars-d-learn mailing list