@@ -7,69 +7,61 @@ namespace SQLitePCL.pretty.Orm
77 internal static class SQLBuilder
88 {
99 internal static string GetTableInfo ( string tableName ) =>
10- "PRAGMA TABLE_INFO(\" " + tableName + " \" )";
10+ $ "PRAGMA TABLE_INFO(\" { tableName } \" )";
1111
1212 internal static string SelectWhereColumnEquals ( string tableName , string columnName ) =>
13- string . Format ( "SELECT * FROM \" {0 }\" WHERE \" {1 }\" = ?" , tableName , columnName ) ;
13+ $ "SELECT * FROM \" { tableName } \" WHERE \" { columnName } \" = ?";
1414
1515 internal static string FindByRowID ( string tableName ) =>
16- string . Format ( "SELECT * FROM \" {0 }\" WHERE ROWID = ?" , tableName ) ;
16+ $ "SELECT * FROM \" { tableName } \" WHERE ROWID = ?";
1717
1818 internal static string AlterTableAddColumn ( string tableName , string columnName , ColumnMapping columnMapping ) =>
19- string . Format ( "ALTER TABLE \" {0 }\" ADD COLUMN {1}" , tableName , SqlDecl ( columnName , columnMapping ) ) ;
19+ $ "ALTER TABLE \" { tableName } \" ADD COLUMN { SqlDecl ( columnName , columnMapping ) } " ;
2020
2121 internal static string DeleteUsingPrimaryKey ( string tableName , string pkColumn ) =>
22- string . Format ( "DELETE FROM \" {0 }\" WHERE \" {1 }\" = ?" , tableName , pkColumn ) ;
22+ $ "DELETE FROM \" { tableName } \" WHERE \" { pkColumn } \" = ?";
2323
24- internal static string InsertOrReplace ( string tableName , IEnumerable < string > columns ) =>
25- string . Format (
26- "INSERT OR REPLACE INTO \" {0}\" ({1}) VALUES ({2})" ,
27- tableName ,
28- string . Join ( "," , columns . Select ( x => "\" " + x + "\" " ) ) ,
29-
30- // FIXME: Might need to quote this for some cases. Test!!!
31- string . Join ( "," , columns . Select ( x => ":" + x ) ) ) ;
32-
33- internal static string CreateIndex ( string indexName , string tableName , IEnumerable < string > columnNames , bool unique )
24+ internal static string InsertOrReplace ( string tableName , IEnumerable < string > columns )
3425 {
35- const string sqlFormat = "CREATE {2} INDEX IF NOT EXISTS \" {3}\" ON \" {0}\" (\" {1}\" )" ;
36- return String . Format ( sqlFormat , tableName , string . Join ( "\" , \" " , columnNames ) , unique ? "UNIQUE" : "" , indexName ) ;
26+ var columnList = string . Join ( "," , columns . Select ( x => $ "\" { x } \" ") ) ;
27+ var bindParamList = string . Join ( "," , columns . Select ( x => $ ":{ x } ") ) ;
28+ return $ "INSERT OR REPLACE INTO \" { tableName } \" ({ columnList } ) VALUES ({ bindParamList } )";
3729 }
3830
31+ internal static string CreateIndex ( string indexName , string tableName , IEnumerable < string > columnNames , bool unique ) =>
32+ $ "CREATE { ( unique ? "UNIQUE" : "" ) } INDEX IF NOT EXISTS \" { indexName } \" ON \" { tableName } \" (\" { string . Join ( "\" , \" " , columnNames ) } \" )";
33+
3934 internal static string NameIndex ( string tableName , IEnumerable < string > columnNames ) =>
40- tableName + "_" + string . Join ( "_" , columnNames ) ;
35+ $ " { tableName } _ { string . Join ( "_" , columnNames ) } " ;
4136
4237 internal static string NameIndex ( string tableName , string columnName ) =>
4338 NameIndex ( tableName , new String [ ] { columnName } ) ;
4439
4540 internal static string ListIndexes ( string tableName ) =>
46- string . Format ( "PRAGMA INDEX_LIST (\" {0 }\" )" , tableName ) ;
41+ $ "PRAGMA INDEX_LIST (\" { tableName } \" )";
4742
4843 internal static string IndexInfo ( string indexName ) =>
49- string . Format ( "PRAGMA INDEX_INFO (\" {0 }\" )" , indexName ) ;
44+ $ "PRAGMA INDEX_INFO (\" { indexName } \" )";
5045
5146 internal static string CreateTableIfNotExists ( string tableName , CreateFlags createFlags , IReadOnlyDictionary < string , ColumnMapping > columns )
5247 {
5348 bool fts3 = ( createFlags & CreateFlags . FullTextSearch3 ) != 0 ;
5449 bool fts4 = ( createFlags & CreateFlags . FullTextSearch4 ) != 0 ;
5550 bool fts = fts3 || fts4 ;
5651
57- var @virtual = fts ? "VIRTUAL " : string . Empty ;
58- var @using = fts3 ? "USING FTS3 " : fts4 ? "USING FTS4 " : string . Empty ;
52+ var @virtual = fts ? "VIRTUAL " : "" ;
53+ var @using = fts3 ? "USING FTS3 " : fts4 ? "USING FTS4 " : "" ;
5954
6055 // Build query.
61- var query = "CREATE " + @virtual + " TABLE IF NOT EXISTS \" " + tableName + " \" " + @using + " (\n ";
56+ var query = $ "CREATE { @virtual } TABLE IF NOT EXISTS \" { tableName } \" { @using } (\n ";
6257 var decls = columns . Select ( c => SQLBuilder . SqlDecl ( c . Key , c . Value ) ) ;
6358 var decl = string . Join ( ",\n " , decls . ToArray ( ) ) ;
6459 query += decl ;
6560 var fkconstraints =
6661 string . Join (
6762 ",\n " ,
6863 columns . Where ( x => x . Value . ForeignKeyConstraint != null ) . Select ( x =>
69- string . Format ( "FOREIGN KEY(\" {0}\" ) REFERENCES \" {1}\" (\" {2}\" )" ,
70- x . Key ,
71- x . Value . ForeignKeyConstraint . TableName ,
72- x . Value . ForeignKeyConstraint . ColumnName ) ) ) ;
64+ $ "FOREIGN KEY(\" { x . Key } \" ) REFERENCES \" { x . Value . ForeignKeyConstraint . TableName } \" (\" { x . Value . ForeignKeyConstraint . ColumnName } \" )") ) ;
7365 query += ( fkconstraints . Length != 0 ) ? "," + fkconstraints : "" ;
7466 query += ")" ;
7567
@@ -78,7 +70,7 @@ internal static string CreateTableIfNotExists(string tableName, CreateFlags crea
7870
7971 internal static string SqlDecl ( string columnName , ColumnMapping columnMapping )
8072 {
81- string decl = "\" " + columnName + " \" " + columnMapping . Metadata . DeclaredType + " ";
73+ string decl = $ "\" { columnName } \" { columnMapping . Metadata . DeclaredType } ";
8274
8375 if ( columnMapping . Metadata . IsPrimaryKeyPart && columnMapping . Metadata . IsAutoIncrement )
8476 {
@@ -90,22 +82,22 @@ internal static string SqlDecl(string columnName, ColumnMapping columnMapping)
9082 }
9183 else if ( columnMapping . Metadata . HasNotNullConstraint )
9284 {
93- decl += "NOT NULL DEFAULT \" " + columnMapping . DefaultValue . ToString ( ) + " \" ";
85+ decl += $ "NOT NULL DEFAULT \" { columnMapping . DefaultValue . ToString ( ) } \" ";
9486 }
9587
9688 if ( ! string . IsNullOrEmpty ( columnMapping . Metadata . CollationSequence ) )
9789 {
98- decl += "COLLATE " + columnMapping . Metadata . CollationSequence + " ";
90+ decl += $ "COLLATE { columnMapping . Metadata . CollationSequence } ";
9991 }
10092
10193 return decl ;
10294 }
10395
10496 internal static string DropTableIfExists ( string tableName ) =>
105- string . Format ( "DROP TABLE IF EXISTS \" {0 }\" " , tableName ) ;
97+ $ "DROP TABLE IF EXISTS \" { tableName } \" ";
10698
10799 internal static string DeleteAll ( string tableName ) =>
108- string . Format ( "DELETE FROM \" {0 }\" " , tableName ) ;
100+ $ "DELETE FROM \" { tableName } \" ";
109101 }
110102}
111103
0 commit comments