@@ -368,15 +368,70 @@ public int CreateTable(Type ty, CreateFlags createFlags = CreateFlags.None)
368
368
369
369
foreach ( var indexName in indexes . Keys ) {
370
370
var index = indexes [ indexName ] ;
371
- const string sqlFormat = "create {3} index if not exists \" {0}\" on \" {1}\" (\" {2}\" )" ;
372
371
var columns = String . Join ( "\" ,\" " , index . Columns . OrderBy ( i => i . Order ) . Select ( i => i . ColumnName ) . ToArray ( ) ) ;
373
- var sql = String . Format ( sqlFormat , indexName , index . TableName , columns , index . Unique ? "unique" : "" ) ;
374
- count += Execute ( sql ) ;
372
+ count += CreateIndex ( indexName , index . TableName , columns , index . Unique ) ;
375
373
}
376
374
377
375
return count ;
378
376
}
379
377
378
+ /// <summary>
379
+ /// Creates an index for the specified table and column.
380
+ /// </summary>
381
+ /// <param name="indexName">Name of the index to create</param>
382
+ /// <param name="tableName">Name of the database table</param>
383
+ /// <param name="columnName">Name of the column to index</param>
384
+ /// <param name="unique">Whether the index should be unique</param>
385
+ public int CreateIndex ( string indexName , string tableName , string columnName , bool unique = false )
386
+ {
387
+ const string sqlFormat = "create {2} index if not exists \" {3}\" on \" {0}\" (\" {1}\" )" ;
388
+ var sql = String . Format ( sqlFormat , tableName , columnName , unique ? "unique" : "" , indexName ) ;
389
+ return Execute ( sql ) ;
390
+ }
391
+
392
+ /// <summary>
393
+ /// Creates an index for the specified table and column.
394
+ /// </summary>
395
+ /// <param name="tableName">Name of the database table</param>
396
+ /// <param name="columnName">Name of the column to index</param>
397
+ /// <param name="unique">Whether the index should be unique</param>
398
+ public int CreateIndex ( string tableName , string columnName , bool unique = false )
399
+ {
400
+ return CreateIndex ( string . Concat ( tableName , "_" , columnName . Replace ( "\" ,\" " , "_" ) ) , tableName , columnName , unique ) ;
401
+ }
402
+
403
+ /// <summary>
404
+ /// Creates an index for the specified object property.
405
+ /// e.g. CreateIndex<Client>(c => c.Name);
406
+ /// </summary>
407
+ /// <typeparam name="T">Type to reflect to a database table.</typeparam>
408
+ /// <param name="property">Property to index</param>
409
+ /// <param name="unique">Whether the index should be unique</param>
410
+ public void CreateIndex < T > ( Expression < Func < T , object > > property , bool unique = false )
411
+ {
412
+ MemberExpression mx ;
413
+ if ( property . Body . NodeType == ExpressionType . Convert )
414
+ {
415
+ mx = ( ( UnaryExpression ) property . Body ) . Operand as MemberExpression ;
416
+ }
417
+ else
418
+ {
419
+ mx = ( property . Body as MemberExpression ) ;
420
+ }
421
+ var propertyInfo = mx . Member as PropertyInfo ;
422
+ if ( propertyInfo == null )
423
+ {
424
+ throw new ArgumentException ( "The lambda expression 'property' should point to a valid Property" ) ;
425
+ }
426
+
427
+ var propName = propertyInfo . Name ;
428
+
429
+ var map = GetMapping < T > ( ) ;
430
+ var colName = map . FindColumnWithPropertyName ( propName ) . Name ;
431
+
432
+ CreateIndex ( map . TableName , colName , unique ) ;
433
+ }
434
+
380
435
public class ColumnInfo
381
436
{
382
437
// public int cid { get; set; }
0 commit comments