Skip to content

Commit

Permalink
Fix: from database access to table by his name, add more example in e…
Browse files Browse the repository at this point in the history
…xamples/test_sql.d
  • Loading branch information
bioinfornatics committed Nov 4, 2011
1 parent 53cf733 commit 847056a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
15 changes: 10 additions & 5 deletions examples/test_sql.d
Expand Up @@ -7,29 +7,34 @@ void main( string[] args ){
Database db = new Database( "myDB.sqlite3.db" );
writeln( "+++ create table" );
db.createTable( "people", "name TEXT", "id INTEGER PRIMARY KEY NOT NULL" );
writeln( "+++ insert table" );
//~ db["people"].insert( cast(Variant[][])[["john"], ["smith"]], ["name"] );
Variant name1 = "john";
Variant name2 = "smith";
Variant id = 1;
writeln( "+++ INSERT INTO people (id, name) VALUES(?, ?), john, 1" );
db["people"].insert( [id, name1], ["id", "name"] );
writeln( "+++ INSERT INTO people (id, name) VALUES(?), smith" );
db["people"].insert( [name2], ["name"] );
writeln( "+++ select" );
writeln( "+++ SELECT name FROM people WHERE id=1" );
Row[] results1 = db["people"].select( ["name"], "id=1" );
writeln( "+++ print" );
writeln(results1.header);
foreach( row; results1 )
writeln( row );
writeln( "+++ select whith binding value" );
writeln( "+++ SELECT name FROM people WHERE id=?, 1" );
Row[] results2 = db["people"].select( ["name"], "id=?", id );
writeln( "+++ print" );
writeln(results2.header);
foreach( row; results2 )
writeln( row );
writeln( "+++ select: manual" );
writeln( "+++ SELECT * FROM people" );
Row[] results3 = db.command( "SELECT * FROM people;" );
writeln( "+++ print" );
writeln(results3.header);
foreach( row; results3 )
writeln( row );
writeln( "+++ CREATE TABLE car ( constructor TEXT, model TEXT, id INTEGER PRIMARY KEY NOT NULL);" );
db.command( "CREATE TABLE car ( constructor TEXT, model TEXT, id INTEGER PRIMARY KEY NOT NULL)" );
writeln( "+++ UPDATE table name stored" );
db.updateTablesList;
assert( db["car"] !is null);
}
9 changes: 5 additions & 4 deletions src/sqlite/database.d
Expand Up @@ -115,14 +115,15 @@ class Database{
}

@property Row[] tables(){
return command( "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name" );
return command( "SELECT name FROM sqlite_master WHERE type='table'" );
}

@property void updateTablesList(){
Row[] result = tables;
foreach( table; tables ){
Column name = table["name"];
string tableName= name.to!(string)();
foreach( table; result ){
Column column = table["name"];
string tableName= to!(string)( column.getValue );
debug writefln( "list table, %s: %s", column.name, tableName );
if( tableName !in _tables)
_tables[ tableName ] = new Table( this, tableName) ;
}
Expand Down
6 changes: 1 addition & 5 deletions src/sqlite/statement.d
Expand Up @@ -311,7 +311,7 @@ struct Row{
size_t index = 0;
Column result = null;
while( isSearching ){
if( _columns.length >= index )
if( _columns.length <= index )
isSearching = false;
else if( _columns[index].name == name ){
isSearching = false;
Expand Down Expand Up @@ -358,10 +358,6 @@ struct Column{
string toString(){
return "%s: %s".format( name, _value );
}

@property T to(T)(){
return _value.get!(T);
}
}

@property string header(Row[] rows){
Expand Down

0 comments on commit 847056a

Please sign in to comment.